0

I'm developing an android application that searches in a database for advertiser names that are near your current position. To do that, it access some web services that are on the company's servers. Basically the application sends the parameters to the web service, and the web service returns the query results. But, some advertiser names have special characters such as "/" or spaces. I tried replacing spaces with %20 and worked fine, but when sending "/" character I receive errors. I tried replacing it with a %2F as I did with the space but is still not working. Any ideas of what should I do?

EDIT

I used as suggested the

String query = URLEncoder.encode("some value", "utf-8");

But now the server is returning an HTTP 404.11 error. I guess what I should fix next is the server code?

Carlos Tirado
  • 297
  • 1
  • 5
  • 20

3 Answers3

3

You don't have to do the URL encoding manually, you can use the Android Libraries, try this:

String query = URLEncoder.encode("something of yours / words", "utf-8");
String url = "https://stackoverflow.com/search?q=" + query;

Reference:

URL encoding in Android

Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • It worked, but now the problem I think is on the server side because it is returning me http 404.11 errors. – Carlos Tirado Jul 06 '13 at 16:02
  • @Blundell Nice solution. Is there any way to just have the String name 'query' show in the URL and still send the characters in the query String? I am trying to send String data in a clickable link via SMS and I want the query parameter to be as short as possible. For example, I am trying to avoid the clickable link looking like this: https://stackoverflow.com/search?q="something+of+yours+/+words. I want the link to look like this: "https://stackoverflow.com/search?q="query" where query is the String "something of yours / words". Is that possible? – AJW Aug 03 '20 at 02:42
  • If it's inside an SMS there is no way to format how it looks sorry, only thing to do is setup a redirect or use something like tinyurl.com to change the url – Blundell Aug 04 '20 at 13:48
0

Use URLEncoder.encode(urlstringwithspecialchar,"utf-8");

arjoan
  • 1,849
  • 2
  • 20
  • 39
0

you need to put the code in try catch block:-

try {
String query = URLEncoder.encode("something of yours / words", "utf-8");
    String url = "http://stackoverflow.com/search?q=" + query;
    } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36