Abhishek
Abhishek

Reputation: 125

IllegalArgumentException in Android

I am using the following code to obtain an Http Response in an Android app.

 HttpGet get = new HttpGet(targetURL); 
 HttpClient httpclient = new DefaultHttpClient();
 HttpResponse response = httpclient.execute(get);
 String responseBody = EntityUtils.toString(response.getEntity());

But HttpResponse response = httpclient.execute(get); is throwing an java.lang.IllegalArgumentException: Host name may not be null

Any help on what is going wrong?

Upvotes: 1

Views: 129

Answers (2)

Ken Wolf
Ken Wolf

Reputation: 23269

You are missing the "scheme" part of your URI. All URIs require a scheme:

http://en.wikipedia.org/wiki/Uniform_resource_identifier#Syntax

The URI syntax consists of a URI scheme name (such as "http", "ftp", "mailto", "crid" or "file") followed by a colon character, and then by a scheme-specific part.

Add http:// on to your String, so make it http://localhost:8080

HttpGet throws an IllegalArgumentException if the URI is invalid, so that's exactly what is happening.

HttpGet documentation

Upvotes: 1

Kaschwenk
Kaschwenk

Reputation: 622

From http://developer.android.com/reference/org/apache/http/client/methods/HttpGet.html#HttpGet%28java.lang.String%29

Your targetUrl is invalid. So maybe the String is wrong. Another possibility is the answer to:

Java Android , HttpGet error - Host name may not be null

There the problem was that the request was changed by the server when it came from a mobile device.

Upvotes: 0

Related Questions