1

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?

3
  • Why are you encoding the URL? Commented Sep 27, 2015 at 14:54
  • 1
    See stackoverflow.com/questions/10786042/… . In your case you should probably just encode the page number (if it may contain special chars) or skip encoding altogether. Commented Sep 27, 2015 at 15:17
  • @headuck, thanks it helped) Commented Sep 27, 2015 at 16:32

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.