user3742241
user3742241

Reputation: 207

MalformedURLException although I have already replaced spaces with %20

String url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+origin+"&destinations="+destination+"&mode=driving&sensor=false&language=en-EN&units=imperial";
url = url.replaceAll(" ", "%20");

Output :

http://maps.googleapis.com/maps/api/distancematrix/xml?origins=150%20Sutter%20St%20San%20Francisco,%20CA,%20United%20States&destinations=1%20Palmer%20Sq%20E
Princeton,%20NJ%2008542&mode=driving&sensor=false&language=en-EN&units=imperial

But I am getting an error saying :

java.net.MalformedURLException: Illegal character in URL

Can some one help me out ..

Upvotes: 19

Views: 57406

Answers (5)

Sunit Mishra
Sunit Mishra

Reputation: 23

Try

str.replaceAll("\\s","%20");

That should fix it

Upvotes: 1

Pregunton
Pregunton

Reputation: 87

java.net.URLEncoder.encode("Hello World", "UTF-8").replaceAll("\\+", "%20"));have a nice day =).

Upvotes: 4

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

(Note: see update below)

Use the URLEncoder class from the java.net package. Spaces are not the only characters that need to be escaped in URLs, and the URLEncoder will make sure that all characters that need to be encoded are properly encoded.

Here's a small example:

String url = "http://...";
String encodedUrl = null;

try {
    encodedUrl = URLEncoder.encode(url, "UTF-8");
} catch (UnsupportedEncodingException ignored) {
    // Can be safely ignored because UTF-8 is always supported
}

Update

As pointed out in the comments and other answers to this question, the URLEncoder class is only safe to encode the query string parameters of a URL. I currently rely on Guava's UrlEscapers to safely encode different parts of a URL.

Upvotes: 17

To Kra
To Kra

Reputation: 3578

Here is the answer which helped me: HTTP URL Address Encoding in Java

Its just using URL & URI objects.

Original aproach mentioned above in this thread with URLEncoder, encoded all characters in url including http:// and using such url was throwing exception in httpConnection - therefore its not a good option. I am now using URL/URI approach mentioned in link, and it's working as expected.

Upvotes: 6

M Abbas
M Abbas

Reputation: 6479

You shouldn't call String.replaceAll to encode an URL, instead you should use java.net.URLEncoder.encode(String s, String enc).

Note that you only need to encode the query string parameters (name and value) and not the entire URL.

When encoding a String using java.net.URLEncoder.encode(String s, String enc), the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • The space character " " is converted into a plus sign "+".
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the
    platform is used.

See URLEncoder

Ex:

String url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + URLEncoder.encode(origin, "UTF-8");

Upvotes: 11

Related Questions