簡體   English   中英

java.lang.IllegalArgumentException:在觸發get請求時,主機名可能不為null

[英]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)

正如@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 ...

在此輸入圖像描述

但是,當我為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();
}
java.lang.IllegalArgumentException: Host name may not be null

問題來自您的網址檢查您的網址"https://s3euwest1.amazonaws.com/developerapplicationtest/cart/list"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM