How to replace space% 20 in url in android in jsonparsing

In my application, I want to replace the space% 20 in my line. I tried this way

String flag1="http://74.208.194.142/admin/upload_image/1342594079_images (2).jpg";
flag1.replaceAll("", "%20");

But it does not work, please help me. I get an exception from a null pointer.

+5
source share
4 answers

You should do it like this:

flag1 = flag1.replaceAll(" ", "%20");

first you put an empty string instead of space. and second you must return the value to a variable flag1.

+8
source
    String flag1="http://74.208.194.142/admin/upload_image/1342594079_images (2).jpg";
    URI uri = null;
    try {
        uri = new URI(flag1.replaceAll(" ", "%20"));
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(uri);

The console displays the output as

http://74.208.194.142/admin/upload_image/1342594079_images%20(2).jpg

+4
source

flag1.replaceAll() . :

String flag1="http://74.208.194.142/admin/upload_image/1342594079_images (2).jpg";
flag1 = flag1.replaceAll(" ", "%20");

1.

+3

Take a look at http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html

Not only spaces need to be replaced. URLs are made up of special characters for special values ​​(for example, a question mark to start a query string)

String flag1 = URLEncoder.encode("This string has spaces", "UTF-8")
+3
source

All Articles