6

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)

2 Answers 2

9

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Good workaround, although the initial problem still strange and unresolved. Bug report?
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.
No luck? You are still having the same issue?
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.
-2
java.lang.IllegalArgumentException: Host name may not be null

Issue came because of your URL check your URL "https://s3­eu­west­1.amazonaws.com/developer­application­test/cart/list"

2 Comments

my bad I have updated my question, i pasted correct url but formatted removed hyphen.
@Techfist so you still got the same error, even with the proper URL now in your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.