Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 990
Techfist Avatar asked Jan 22 '15 16:01

Techfist


1 Answers

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();
}
like image 140
Willis Avatar answered Nov 16 '22 06:11

Willis