Carlos Tirado
Carlos Tirado

Reputation: 397

How to send special characters to web services?

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?

Upvotes: 0

Views: 2783

Answers (3)

Amit Jayaswal
Amit Jayaswal

Reputation: 1723

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();
}

Upvotes: 0

arjoan
arjoan

Reputation: 1849

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

Upvotes: 0

Blundell
Blundell

Reputation: 76458

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

Upvotes: 3

Related Questions