我试图从网站获得身份验证。我正在使用AsyncHttpClient
. 这是我正在尝试的代码。
这是我的代码,
public class LoginActivity extends Activity {
String tag = "LoginActivity";
Button requestBtn;
AsyncHttpClient httpClient = new AsyncHttpClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
requestBtn = (Button) findViewById(R.id.upload_file);
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
httpClient.setCookieStore(myCookieStore);
httpClient.setBasicAuth(ApplicationConstants.userName,
ApplicationConstants.password, new AuthScope(
"http://*.*.*.*:8080/someUrl", 8080,
AuthScope.ANY_REALM));
requestBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
httpClient.get("http://*.*.*.*:8080/someurl",new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
Log.d("Sucessful upload","Onsucess" + response);
}
@Override
public void onFailure(Throwable arg0,String arg1) {
Log.d("LoginActivity",arg0.toString());
arg0.printStackTrace();
super.onFailure(arg0, arg1);
}
});
}
}
});
}
}
当我点击按钮时出现异常。它说身份验证失败异常Logcat:
02-27 16:02:42.930: D/LoginActivity(8869): org.apache.http.client.HttpResponseException: Unauthorized
02-27 16:02:42.930: W/System.err(8869): org.apache.http.client.HttpResponseException: Unauthorized
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:235)
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:79)
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:95)
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:57)
02-27 16:02:42.930: W/System.err(8869): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
02-27 16:02:42.930: W/System.err(8869): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-27 16:02:42.930: W/System.err(8869): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-27 16:02:42.940: W/System.err(8869): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-27 16:02:42.940: W/System.err(8869): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-27 16:02:42.940: W/System.err(8869): at java.lang.Thread.run(Thread.java:856)
我做错了什么?我使用的网址是正确的。