0

I have a method

String Address=search.getText().toString();

private String getAddressUrl() {
     String mapUrl="http://maps.googleapis.com/maps/api/geocode/json?";
     String add=Address; //Address is a String input by the user  
     String baseUrl1=mapUrl+"address="+add+"&sensor=true";
return baseUrl1;
}

If I input the string like "170 william street New York, NY" I am getting illegal character at query error because it contains spaces.

Is there any method to insert %20 for spaces in Address string

vidstige
  • 11,888
  • 8
  • 65
  • 104
Sunny
  • 13,864
  • 15
  • 78
  • 126
  • Check out http://stackoverflow.com/questions/573184/java-convert-string-to-valid-uri-object – skyuzo Nov 20 '11 at 09:12
  • on a side note - Try using beginning lower case letters for local variable names and fields like `Address`, this will make it easier for other java programmers to read your code. – vidstige Nov 20 '11 at 09:24

3 Answers3

5

Look at Uri.encode(String).

String add=Uri.encode(Address);
kabuko
  • 35,577
  • 9
  • 76
  • 92
1

You should use class UrlEncoder

Eugen Martynov
  • 19,222
  • 10
  • 57
  • 112
  • A little gotcha with URLEncoder: It will encode spaces as `+` instead of `%20`. – Muz Oct 09 '12 at 04:50
0

Oops, thought this was .Net for some reason.

Andy
  • 11
  • 1