yerassyl
yerassyl

Reputation: 3048

URLEncoder: encoding string in android

I am constructing String in my android app and then pass it to URLEncoder.

String searchStr;
// then I get some searchStr from shared preferences
// i check it is correct
searchStr = searchStr + "/page/"+pageNumber+"";

Then I pass that searchStr to URL encoder:

try {
  String url_params = URLEncoder.encode(params[0]);
  String result = downloadURL("some url/" + url_params);
  Log.d("key", url_params); // -> php/page/10
}catch (IOException){
  Log.d("key", e.getMessage() );
}

So here i get IOException which shows me this message:

http://some url/php%2Fpage%2F10

If I copy paste it to browser it shows me Not found error with this message:

The requested url  someurl/php/page/10 was not found on this server

Also if I change those strange sign %2F into / in browser I am able to get the page. So how can I construct proper string with / instead of %2F sign?

Upvotes: 1

Views: 3535

Answers (1)

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

You don't encode the entire URL, only parts of it that come from "unreliable sources".

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

Upvotes: 2

Related Questions