10

I get the error "Target host must not be null, or set in parameters".

  • I DO have Internet permission in my manifest file
  • I have put 'http://' before my Url
  • I DO encode the URL

This is my code:

   String url = "http://maps.google.com/maps/api/directions/json?origin=1600 Pennsylvania Avenue NW, Washington, DC 20500&destination=1029 Vermont Ave NW, Washington, DC 20005&sensor=false";
   HttpClient httpclient = new DefaultHttpClient();
   String goodURL = convertURL(url);//change weird characters for %etc
   HttpPost httppost = new HttpPost(goodURL);
   HttpResponse response = httpclient.execute(httppost);

In 5th line (last line above), my program throws an exception. here is the exact error:

java.lang.IllegalArgumentException: Host name may not be null

I Do encode my string in method convertURL...

goodURL= http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false

Any suggestions? Thanks!

cafesanu
  • 435
  • 1
  • 4
  • 12
  • You cannot have spaces in the URL! let me see how are you encoding your URL – Samer Sep 21 '12 at 20:18
  • Your problem is probably in your convertURL function... use a debugger and check to see what that is returning. – Joel Sep 21 '12 at 20:19
  • Hi! I was passing the whole URL, I modifyed it but it's still giving me the error... goodURL= `http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false` – cafesanu Sep 21 '12 at 21:27
  • Ok, so you guys were right! I was passing the whole URL... I fixed it but now it gives me this exception! android.os.NetworkOnMainThreadException but I can research what this is about... Thanks! – cafesanu Sep 21 '12 at 21:34

3 Answers3

5

I'm not sure what your URL encode method is doing, but if you are using a method from the framework like URLEncoder, you should never pass the full URL, just the parameters list you need to encode to escape special characters.

Encoding the full URL will percent escape every character, including the :// into %3A%2F%2F and all additional slashes into %2F.

Take a look at the value of your goodUrl string after you encode it.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Hi! I was passing the whole URL, I modifyed it but it still giving me an error.... goodURL is `http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false` – cafesanu Sep 21 '12 at 21:15
  • Ok, so you guys were right! I was passing the whole URL... I fixed it but now it gives me this exception! android.os.NetworkOnMainThreadException but I can research what this is about... Thanks! – cafesanu Sep 21 '12 at 21:33
  • 2
    "NetworkOnMainThreadException" means exactly what it says. Android does not allow you to do network access from the main thread because it will block the UI; you must create a background thread for network I/O. You can learn more from this training article: http://developer.android.com/training/basics/network-ops/index.html – devunwired Sep 21 '12 at 21:42
1

Just use:

URLEncoder.encode(YOUR_STRING);
Samer
  • 536
  • 3
  • 5
1

Encode your URL string before you post the request, but only encode the parameters after the ?:

String url = "http://maps.google.com/maps/api/directions/json?";
String params = "origin=1600 Pennsylvania Avenue NW, Washington, DC 20500&destination=1029 Vermont Ave NW, Washington, DC 20005&sensor=false";
HttpClient httpclient = new DefaultHttpClient();
String goodParams = convertURL(params);//change weird characters for %etc
HttpPost httppost = new HttpPost(url + goodParams);
HttpResponse response = httpclient.execute(httppost);
AWT
  • 3,657
  • 5
  • 32
  • 60
  • I wonder then if URLEncode is encoding everything in that string, including the host name. In your example above, what does goodURL look like? Instead of String, try using the URL object, and set the host name and resources in that. – AWT Sep 21 '12 at 20:42
  • Hi! I was passing the whole URL, I modifyed it but it still giving me an error.... goodURL is `http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false` – cafesanu Sep 21 '12 at 21:16
  • Ok, so you guys were right! I was passing the whole URL... I fixed it but now it gives me this exception! android.os.NetworkOnMainThreadException but I can research what this is about... Thanks! – cafesanu Sep 21 '12 at 21:33
  • 3
    NetworkOnMainThreadException is thrown when you try to make a network call on the main thread, since this would affect the responsiveness of the UI and result in a bad user experience. You'll need to handle the send and receive in a background thread. – AWT Sep 22 '12 at 23:36