我无法使用loopj Asnctask Http库在HTTPS中传递身份验证。如何实现它以及我需要将哪些内容添加到我的代码中。
我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etLogin = (EditText) findViewById(R.id.etLogin);
etPassword = (EditText) findViewById(R.id.etPassword);
btnJoin = (Button) findViewById(R.id.btnJoin);
String username = etLogin.getText().toString();
String password = etLogin.getText().toString();
final String url = "https://IpAddress:Port/index.php/api/user";
final AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth(username,password, new AuthScope("IP", Port, AuthScope.ANY_REALM));
client.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.DEFAULT));
btnJoin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d("App", "Connected");
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("App","Upload Failed!");
Log.d("App","Error Message: " + error.getMessage());
Log.d("App", "Error Cause: " + error.getCause());
}
});
}
});
他在日志中写道:
Log.d:
09-06 05:20:27.471 1254-1254/uz.istv.jsonparsingtestapp D/App: Upload Failed!
09-06 05:20:27.471 1254-1254/uz.istv.jsonparsingtestapp D/App: Error Message: UnknownHostException exception: Unable to resolve host "host5.istv.uz": No address associated with hostname
09-06 05:20:27.471 1254-1254/uz.istv.jsonparsingtestapp D/App: Error Cause: null
所有日志:
09-06 05:20:27.407 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
09-06 05:20:27.407 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-06 05:20:27.415 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.net.InetAddress.getByName(InetAddress.java:289)
09-06 05:20:27.415 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:464)
09-06 05:20:27.423 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.conn.scheme.SchemeSocketFactoryAdaptor.connectSocket(SchemeSocketFactoryAdaptor.java:65)
09-06 05:20:27.427 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
09-06 05:20:27.431 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:145)
09-06 05:20:27.431 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:131)
09-06 05:20:27.443 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
09-06 05:20:27.443 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
09-06 05:20:27.451 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
09-06 05:20:27.455 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
09-06 05:20:27.455 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
09-06 05:20:27.455 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
09-06 05:20:27.459 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
09-06 05:20:27.459 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
09-06 05:20:27.463 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-06 05:20:27.463 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-06 05:20:27.467 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-06 05:20:27.467 1254-1282/uz.istv.jsonparsingtestapp W/System.err: at java.lang.Thread.run(Thread.java:856)
提前致谢