我正在使用CommonsWare的Android教程,无法弄清楚为什么我会收到错误。这是我的代码:
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();
}
};
} 这是我的LogCat错误消息:
08-11 11:52:34.769:ERROR / Patchy.java(4738):updateStatus()中的异常 08-11 11:52:34.769:ERROR / Patchy.java(4738):org.apache.http.client.HttpResponseException:未经授权 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):在android.view.View.performClick(View.java:2365) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.view.View.onTouchEvent(View.java:4180) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.widget.TextView.onTouchEvent(TextView.java:6696) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.view.View.dispatchTouchEvent(View.java:3710) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在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):在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):在android.view.ViewRoot.handleMessage(ViewRoot.java:1696) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.os.Handler.dispatchMessage(Handler.java:99) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在android.os.Looper.loop(Looper.java:130) 08-11 11:52:34.769:ERROR / Patchy.java(4738):在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)
在我的手机上弹出一个对话框,显示错误“异常.org.apache.http.client.HttpResponseException:Unauthorized”
有什么想法吗?
答案 0 :(得分:0)
关于CommonsWare的问题应该转到cw-android Google Group。我几乎错过了这个问题。
确保输入正确的用户名和密码。此外,请确保您单击了来自identi.ca的电子邮件中的链接 - 在完成此操作之前,您无法使用该API。