问题描述
- AsyncHttpClient身份验证失败
-
使用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); } }); } } });
}
}结果报出异常:
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)
时间: 2024-12-22 21:38:58