Search code examples
javaandroidhttpreplaceall

Replacing symbol in URL won't Work


I am trying to display an image in android by receiving the url from the web server and then turning it to a bitmap image but getting the following error as the symbol %5C is in it.

E/Error﹕ http:%5C/%5C/thumbs3.ebaystatic.com%5C/pict%5C/3007385805144040_5.jpg

I have tried url2.replaceAll("%5C",""); to get rid of the symbol but this has no effect at all. How can I get rid of it so I have a valid url.


Solution

  • What you are looking for is a called URL Decoding.

    Read more here: How to do URL decoding in Java?

    Don't try manually replacing yourself, use a library or write your own for all cases:

    import java.net.URLDecoder;
    
    String result = URLDecoder.decode(url, "UTF-8");
    

    Java 1.7+:

    import java.net.URLDecoder;
    import java.nio.charset.StandardCharsets;
    
    String result = URLDecoder.decode(url, StandardCharsets.UTF_8.name());