正如@atish shimpi 所提到的,这很可能是由于 URL 格式不正确。我输入了以下代码,并在开发手机上对其进行了调试:
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 有关。无论如何,我最终能够通过获取您提供的链接并手动创建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();
}