2

I am getting this error:

java.net.MalformedURLException: Protocol not found[java.lang.StringBuilder] 

When the following line is getting executed:

url = new URL(urlString.toString());

urlString stores the following value:

http://maps.google.com/maps?f=d&hl=en&saddr=25.04202,121.534761&daddr=25.05202,121.554761&ie=UTF8&0&om=0&output=kml

What causes this Exception?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
ManJan
  • 3,939
  • 3
  • 20
  • 22
  • Try logging the urlString.toString() value as the parser complains that it starts with the value "java.lang.StringBuilder" – MahdeTo Feb 23 '12 at 07:26

4 Answers4

6

Chances are that you didn't clean after changing from

url = new URL( urlString );

to

url = new URL(urlString.toString());

You should log the value of the parameter passed to the constructor of URL. It's not what you think it should be.

urlString would print a value in the form of java.lang.StringBuilder@ thus throwing the exception if you try to build a url out of that.

But using to String will print the value of the content string built by the stringbuilder.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • yes when I print it shows url=[Ljava.lang.StringBuilder;@412b0fb0, but when I check its value while debugging it shows the proper URL. What do u mean by logging the value of the param to the constructor of URL? What else should I do? – ManJan Feb 23 '12 at 08:04
  • I meant log the value of the parameter *passed* to the constructor of URL. Did you clean the project. For sure, the problems comes from here. – Snicolas Feb 23 '12 at 11:20
  • Thank you so much Snicolas.. I figured out the problem. I was using AsyncTask and was not passing params correctly. Its now resolved :) – ManJan Feb 24 '12 at 01:40
3

Here is an example with exception handling ...

String urlString = "https://www.example.com/";
URL url = null;

// handle Exception
try {
    url = new URL(urlString);
} catch (MalformedURLException e) {
    System.out.println("The URL is not valid.");
    System.out.println(e.getMessage());
}

// print
if (url != null) {
    System.out.println(url.toString());
}
Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
0

You can also get this exception if your network firewall blocking that URL.

W00di
  • 954
  • 12
  • 21
0

replace url with php file address placed over server instedof html

raj
  • 2,088
  • 14
  • 23