【问题标题】:AndroidHttpClient.execute is returning httpresponse as null [duplicate]AndroidHttpClient.execute 将 httpresponse 返回为 null [重复]
【发布时间】:2013-09-20 07:30:54
【问题描述】:

我正在尝试从以下代码访问我的 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 始终为空。甚至没有抛出异常。 我已经检查了像https://google.co.in/ 这样的有效uri,它工作正常。 我的浏览器 URI 也工作正常。 由于没有抛出异常,我无法继续进行。

【问题讨论】:

  • 顺便说一句,如果您不需要任何参数,您可以使用 Void 扩展您的 AsyncTask,例如 AsyncTask<Void, Void, Void>

标签: android rest android-asynctask


【解决方案1】:

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

       http://10.0.2.2/

而不是

        http://localhost/

对于reference

【讨论】:

  • +1, localhost 在你的开发机器上对应模拟器自带的环回接口。如果你想访问在你的开发机器的环回接口上运行的服务,你应该使用特殊地址10.0.2.2来代替
  • 为什么它显示 400:错误请求...我的服务在本地主机上运行。我从带有路径的android模拟器调用它:10.0.2.2:52511/api/Values/GetProduct/5
【解决方案2】:

要在模拟器中访问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

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 2019-10-04
    • 2011-11-01
    • 2017-05-09
    • 2012-02-01
    • 2015-09-11
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多