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
  • 12,492
  • 9
  • 66
  • 110
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • 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
  • 36,028
  • 10
  • 80
  • 93
1

You should use class UrlEncoder

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • 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