【问题标题】:Android Host name may not be nullAndroid 主机名不能为空
【发布时间】:2014-04-01 04:41:22
【问题描述】:

我知道这个问题已经被问过很多次了,但是这些问题的答案对我没有用。

我正在尝试访问 REST API,但是当我尝试连接时出现此错误:

java.lang.IllegalArgumentException: Host name may not be null

我的网址是“http:localhost:3000/api/v1/users”,

我的代码:

public String post(List<NameValuePair> data) {
    String result = null;
    HttpPost request;
    try {
        request = new HttpPost("http:localhost:3000/api/v1/users");
        ResponseHandler<String> handler = new BasicResponseHandler();
        try {
            result = client.execute(request, handler);
            client.getConnectionManager().shutdown();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return result;
}

【问题讨论】:

  • 你在浏览器中检查这个 http:localhost:3000/api/v1/users 吗?

标签: android rest httpclient


【解决方案1】:

android 不理解localhost。因此将localhost 替换为10.0.2.2,然后尝试。它应该工作。

试试这个

http://10.0.2.2:3000/api/v1/users

【讨论】:

    【解决方案2】:

    试试这个

    http://10.0.2.2:3000/api/v1/users
    

    Android 不理解 localhost 行 pc 浏览器。

    【讨论】:

      【解决方案3】:

      乍一看,我认为 URL 的传递存在格式问题

      • 更改 url

      http:localhost:3000/api/v1/users

      http://localhost:3000/api/v1/users
      

      编辑

        And yes if you are running the emulator it is already taken the local host 
      

      this 提供更多信息。

      【讨论】:

        猜你喜欢
        • 2020-09-19
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多