问题
I am trying to access my uri from the below code
AsyncTask asyncTask = new AsyncTask() {
@Override
protected Object doInBackground(Object... params) {
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("");
HttpUriRequest uriGetRequest = new HttpGet("http://localhost:8080/restsql-0.8.6/res/");
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(uriGetRequest);
;
} catch (Exception e) {
// TODO Auto-generated catch block
if(e!=null){
Log.e("ModelLoader", e.getMessage(), e);
}
Log.e("Model Loader", "Error while calling execute");
}
return httpResponse;
}
};
asyncTask.execute(new Object());
But always the httpresponse is null. Even no exception is thrown. I have checked with a valid uri like https://google.co.in/ and its working fine. Also my URI from browser is working fine . As there is no exception thrown I am not able to proceed further.
回答1:
In AVD to connect to localhost you need to use url
http://10.0.2.2/
instead of
http://localhost/
For reference.
回答2:
To access localhost
in emulator use 10.0.2.2
http://localhost:8080/restsql-0.8.6/res/
should be
http://10.0.2.2:8080/restsql-0.8.6/res/
Check the docs
来源:https://stackoverflow.com/questions/18823107/androidhttpclient-execute-is-returning-httpresponse-as-null