【问题标题】:java.lang.IllegalArgumentException: Host name may not be null, while firing a get requestjava.lang.IllegalArgumentException:主机名可能不为空,同时触发获取请求
【发布时间】:2015-03-21 13:57:46
【问题描述】:

感谢您对此提供的帮助,以下是正在尝试执行的代码,但得到的只是这个异常,我做了很多更改,但无法解决这个问题。

如果您有任何指示,请告诉我,我在 android 4.4.4 上运行

HttpGet request = new HttpGet("https://s3­-eu-­west-­1.amazonaws.com/developer-application-­test/cart/list");
resp = client.execute(request);

01-22 22:25:03.885: W/System.err(14697): java.lang.IllegalArgumentException: Host name may not be null
01-22 22:25:03.886: W/System.err(14697):    at org.apache.http.HttpHost.<init>(HttpHost.java:83)
01-22 22:25:03.886: W/System.err(14697):    at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:508)
01-22 22:25:03.886: W/System.err(14697):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
01-22 22:25:03.886: W/System.err(14697):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)

【问题讨论】:

    标签: java android apache http


    【解决方案1】:

    正如@atish shimpi 所提到的,这很可能是由于 URL 格式不正确。我输入了以下代码,并在开发手机上对其进行了调试:

    HttpClient httpClient = new DefaultHttpClient();
    URI url = new URI("https://s3-eu­west­1.amazonaws.com/developer-application­test/cart/list");
    URI url1 = new URI("https://www.google.com/");
    HttpGet httpGet = new HttpGet(url);
    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity httpEntity = httpResponse.getEntity();
    

    如您所见,我添加了另一个指向https://www.google.com/URI 对象用作比较。当我调试时,我在 URI 对象的创建上设置了断点。为您提供的地址创建相应的URI 对象后,host 字段为null...

    但是,当我为 Google 地址创建类似的 URI 对象时,host 字段不是 null,这意味着您的地址有问题...

    我仍然不太清楚为什么URI(String spec) 方法无法解析正确的字段。这可能是一个错误,也可能只是与您的特定 URL 有关。无论如何,我最终能够通过获取您提供的链接并手动创建URI 对象来处理请求,如下所示:

    URI uri = new URI("https", "s3-eu-west-1.amazonaws.com", "/developer-application-test/cart/list", null, null);
    

    使用这个手动创建的URI,我能够下载你创建的列表:

    "products" : [
        {
        "product_id" : "1",
        "name" : "Apples",
        "price" : 120,
        "image" : "https://s3-eu-west-1.amazonaws.com/developer-application-test/images/1.jpg"
        },
        {
        "product_id" : "2",
        "name" : "Oranges",
        "price" : 167,
        "image" : "https://s3-eu-west-1.amazonaws.com/developer-application-test/images/2.jpg"
        },
        {
        "product_id" : "3",
        "name" : "Bananas",
        "price" : 88,
        "image" : "https://s3-eu-west-1.amazonaws.com/developer-application-test/images/3.jpg"
        },
    etc....
    

    作为参考,这是我的最终工作代码:

    try
    {
        HttpClient httpClient = new DefaultHttpClient();
        URI uri = new URI("https", "s3-eu-west-1.amazonaws.com", "/developer-application-test/cart/list", null, null);
        HttpGet httpGet = new HttpGet(uri);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null)
        {
            InputStream inputStream = httpEntity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String currentLine = null;
            while ((currentLine = bufferedReader.readLine()) != null)
            {
                stringBuilder.append(currentLine + "\n");
            }
            String result = stringBuilder.toString();
            Log.v("Http Request Results:",result);
            inputStream.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    

    【讨论】:

    • 很好的解决方法,尽管最初的问题仍然很奇怪且未解决。错误报告?
    • 这太愚蠢了,按部分制作 URI 确实有助于使 uri 正确,但仍然无法一次性正确解析 uri。
    • 运气不好?你还有同样的问题吗?
    • 我们遇到了同样的问题。这似乎是一个“-”和“_”的问题。我们用更简单的 url 创建了一个新的虚拟主机,这个问题就消失了。注意:这不是解决方案。我只是给我 2 美分来找出原因可能是什么。
    【解决方案2】:
    java.lang.IllegalArgumentException: Host name may not be null
    

    问题是因为你的 URL 检查你的 URL "https://s3­eu­west­1.amazonaws.com/developer­application­test/cart/list"

    【讨论】:

    • 我的错误我已经更新了我的问题,我粘贴了正确的 url 但格式化删除了连字符。
    • @Techfist 所以你仍然遇到同样的错误,即使你的问题中现在有正确的 URL?
    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多