java.lang.IllegalArgumentException: Host name may not be null, while firing a get request

18,219

As @atish shimpi mentioned, this is most likely due to an improperly formatted URL. I typed up the following code snipped and debugged it on a development phone:

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();

As you can see, I added another URI object which points to https://www.google.com/ to use as a comparison. When I debugged, I set breakpoints on the creation of both URI objects. Upon creating the corresponding URI object for the address you provided, the host field is null...

enter image description here

However, when I create a similiar URI object for the Google address, the host field is not null, which means something is wrong with your address...

enter image description here

I am still not quite sure why the method URI(String spec) fails to resolve the proper fields. This might be a bug or it might just be related to your specific URL. Regardless, I was able to finally process the request by taking the link you provided and manually creating a URI object as follows:

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

Using this manually created URI, I was able to download the list that you created:

"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....

As a reference point, here is my final working code:

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();
}
Share:
18,219

Related videos on Youtube

Techfist
Author by

Techfist

passionate about android, crazy over my playstation, love zooming in on my bike.

Updated on September 15, 2022

Comments

  • Techfist
    Techfist over 1 year

    I will appreciate your help on this, below is the code am trying to execute, but all am getting is this exception, I did many changes, but am not able to resolve this.

    Please let me know if you have any pointers, am running on 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)
    
  • Techfist
    Techfist about 9 years
    my bad I have updated my question, i pasted correct url but formatted removed hyphen.
  • shkschneider
    shkschneider about 9 years
    @Techfist so you still got the same error, even with the proper URL now in your question?
  • shkschneider
    shkschneider about 9 years
    Good workaround, although the initial problem still strange and unresolved. Bug report?
  • Techfist
    Techfist about 9 years
    this is so stupid, making URI by part does helped in getting uri right, but still in one shot system is not able to parse the uri correct.
  • Willis
    Willis about 9 years
    No luck? You are still having the same issue?
  • Ivan Morgillo
    Ivan Morgillo over 8 years
    We had the same issue. It seems to be a "-" and "_" issue. We created a new vhost with a simpler url and this issues disappeared. Note: this is not a solution. I'm just giving my 2 cents to figure out what the cause could be.