AndroidHttpClient.execute将httpresponse返回为null

时间:2013-09-16 08:04:53

标签: android rest android-asynctask

我正在尝试从以下代码访问我的uri

    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());

但httpresponse总是为空。甚至也没有例外。 我已经检查过有效的uri,如https://google.co.in/,并且工作正常。 我的浏览器URI也正常工作。 因为没有例外,我无法继续下去。

2 个答案:

答案 0 :(得分:2)

  

在AVD中连接到localhost,您需要使用url

       http://10.0.2.2/

而不是

        http://localhost/

reference

答案 1 :(得分:0)

要在模拟器中使用localhost

访问10.0.2.2
http://localhost:8080/restsql-0.8.6/res/

应该是

http://10.0.2.2:8080/restsql-0.8.6/res/

检查docs