java.lang.IllegalArgumentException: 主机名不能为空,同时触发 get 请求
java.lang.IllegalArgumentException: Host name may not be null, while firing a get request
非常感谢你的帮助,下面是我正在尝试执行的代码,但我得到的只是这个异常,我做了很多更改,但无法解决这个问题。
如果您有任何指示,请告诉我,运行 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.lang.IllegalArgumentException: Host name may not be null
问题是因为你的 URL 检查你的 URL "https://s3euwest1.amazonaws.com/developerapplicationtest/cart/list"
如@atish shimpi 所述,这很可能是由于格式不正确 URL。我输入了以下代码并在开发 phone:
中对其进行了调试
HttpClient httpClient = new DefaultHttpClient();
URI url = new URI("https://s3-euwest1.amazonaws.com/developer-applicationtest/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 有关。无论如何,我最终能够通过获取您提供的 link 并手动创建一个 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();
}
非常感谢你的帮助,下面是我正在尝试执行的代码,但我得到的只是这个异常,我做了很多更改,但无法解决这个问题。
如果您有任何指示,请告诉我,运行 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.lang.IllegalArgumentException: Host name may not be null
问题是因为你的 URL 检查你的 URL "https://s3euwest1.amazonaws.com/developerapplicationtest/cart/list"
如@atish shimpi 所述,这很可能是由于格式不正确 URL。我输入了以下代码并在开发 phone:
中对其进行了调试HttpClient httpClient = new DefaultHttpClient();
URI url = new URI("https://s3-euwest1.amazonaws.com/developer-applicationtest/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 有关。无论如何,我最终能够通过获取您提供的 link 并手动创建一个 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();
}