AndroidHttpClient.execute is returning httpresponse as null [duplicate]

爷,独闯天下 提交于 2020-01-17 14:34:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!