java.lang.IllegalArgumentException:在触发get请求时,主机名可能不为null

时间:2015-01-22 16:59:40

标签: java android apache http

我将非常感谢您对此的帮助,下面是我正在尝试执行的代码,但我得到的是这个例外,我做了很多更改,但我无法解决这个问题。

如果您有任何指示,请告诉我,我在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)

2 个答案:

答案 0 :(得分:6)

正如@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();

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

enter image description here

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

enter image description here

我仍然不太清楚为什么方法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();
}

答案 1 :(得分:-1)

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

问题出现是因为您的网址检查了您的网址"https://s3­eu­west­1.amazonaws.com/developer­application­test/cart/list"