I am working through an Android tutorial from CommonsWare and cannot figure out why I am gettting my error. Here is my code:
public class Patchy extends Activity {
public String DEB_TAG = "Patchy.java";
private DefaultHttpClient client = null;
private EditText user = null;
private EditText password = null;
private EditText status = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(DEB_TAG, "inside oncreate");
user = (EditText)findViewById(R.id.user);
password = (EditText)findViewById(R.id.password);
status = (EditText)findViewById(R.id.status);
Button send = (Button)findViewById(R.id.send);
send.setOnClickListener(onSend);
client = new DefaultHttpClient();
}
@Override
public void onDestroy() {
super.onDestroy();
client.getConnectionManager().shutdown();
}
private String getCredentials() {
String u = user.getText().toString();
String p = password.getText().toString();
Log.d(DEB_TAG, "Value of u and p is " + u + "..." + p);
String temp = Base64.encodeBytes((u+":"+p).getBytes());
Log.d(DEB_TAG, "Value of temp is " + temp);
return temp;
}
private void updateStatus() {
try {
String s = status.getText().toString();
Log.d(DEB_TAG, "Value of status is " + s);
HttpPost post = new HttpPost("https://identi.ca/api/statuses/update.json");
post.addHeader("Authorization", "Basic" +getCredentials());
List<NameValuePair> form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("status", s));
post.setEntity(new UrlEncodedFormEntity(form, HTTP.UTF_8));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(post, responseHandler);
JSONObject response = new JSONObject(responseBody);
}catch (Throwable t) {
Log.e(DEB_TAG, "Exception in updateStatus()", t);
goBlooey(t);
}
}
private void goBlooey(Throwable t){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
private View.OnClickListener onSend = new View.OnClickListener() {
@Override
public void onClick(View v) {
updateStatus();
}
};
} and here is my LogCat error msgs:
08-11 11:52:34.769: ERROR/Patchy.java(4738): Exception in updateStatus() 08-11 11:52:34.769: ERROR/Patchy.java(4738): org.apache.http.client.HttpResponseException: Unauthorized 08-11 11:52:34.769: ERROR/Patchy.java(4738): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at example.com.patchy.Patchy.updateStatus(Patchy.java:83) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at example.com.patchy.Patchy.access$0(Patchy.java:66) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at example.com.patchy.Patchy$1.onClick(Patchy.java:106) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.View.performClick(View.java:2365) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.View.onTouchEvent(View.java:4180) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.widget.TextView.onTouchEvent(TextView.java:6696) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.View.dispatchTouchEvent(View.java:3710) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1111) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.view.ViewRoot.handleMessage(ViewRoot.java:1696) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.os.Handler.dispatchMessage(Handler.java:99) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.os.Looper.loop(Looper.java:130) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at android.app.ActivityThread.main(ActivityThread.java:4425) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at java.lang.reflect.Method.invokeNative(Native Method) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at java.lang.reflect.Method.invoke(Method.java:521) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 08-11 11:52:34.769: ERROR/Patchy.java(4738): at dalvik.system.NativeStart.main(Native Method)
On my phone a dialog pops up with the error "Exception. org.apache.http.client.HttpResponseException: Unauthorized"
Any ideas?