sivaraj
sivaraj

Reputation: 1867

how to url encode in android?

I am using grid view for displaying image using xml parsing,i got some exception like

java.lang.IllegalArgumentException: Illegal character in path at index 80: http://www.theblacksheeponline.com/party_img/thumbspps/912big_361999096_Flicking Off Douchebag.jpg

How to solve this problem? I want to display all kind of url,anybody knows please give sample code for me.

Thanks All

Upvotes: 17

Views: 72971

Answers (8)

Krunal Shah
Krunal Shah

Reputation: 1436

I tried with URLEncoder that added (+) sign in replace of (" "), but it was not working and getting 404 url not found error.

Then i googled for get better answer and found this and its working awesome.

String urlStr = "http://www.example.com/test/file name.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

This way of encoding url its very useful because using of URL we can separate url into different part. So, there is no need to perform any string operation.

Then second URI class, this approach takes advantage of the URI class feature of properly escaping components when you construct a URI via components rather than from a single string.

Upvotes: 1

Elango
Elango

Reputation: 81

you can use below method

public String parseURL(String url, Map<String, String> params)
{
    Builder builder = Uri.parse(url).buildUpon();
    for (String key : params.keySet())
    {
        builder.appendQueryParameter(key, params.get(key));
    }
    return builder.build().toString();
}

Upvotes: 1

Dhruv Raval
Dhruv Raval

Reputation: 5024

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

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

Upvotes: 4

Jedo
Jedo

Reputation: 811

Also you can use this

private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
String urlEncoded = Uri.encode(path, ALLOWED_URI_CHARS);

it's the most simple method

Upvotes: 16

Daniel Murphy
Daniel Murphy

Reputation: 882

I recently wrote a quick URI encoder for this purpose. It even handles unicode characters. http://www.dmurph.com/2011/01/java-uri-encoder/

Upvotes: 0

Oleg Smirnov
Oleg Smirnov

Reputation: 106

URLEncoder should be used only to encode queries, use java.net.URI class instead:

URI uri = new URI(
    "http",
    "www.theblacksheeponline.com", 
    "/party_img/thumbspps/912big_361999096_Flicking Off Douchebag.jpg",
    null);
String request = uri.toASCIIString();

Upvotes: 1

BitMask777
BitMask777

Reputation: 2733

As Ben says in his comment, you should not use URLEncoder.encode to full URLs because you will change the semantics of the URL per the following example from the W3C:

The URIs http://www.w3.org/albert/bertram/marie-claude and http://www.w3.org/albert/bertram%2Fmarie-claude are NOT identical, as in the second case the encoded slash does not have hierarchical significance.

Instead, you should encode component parts of a URL independently per the following from RFC 3986 Section 2.4

Under normal circumstances, the only time when octets within a URI are percent-encoded is during the process of producing the URI from its component parts. This is when an implementation determines which of the reserved characters are to be used as subcomponent delimiters and which can be safely used as data. Once produced, a URI is always in its percent-encoded form.

So, in short, for your case you should encode/escape your filename and then assemble the URL.

Upvotes: 6

Cpt.Ohlund
Cpt.Ohlund

Reputation: 2679

URL encoding is done in the same way on android as in Java SE;

try {
    String url = "http://www.example.com/?id=123&art=abc";
    String encodedurl = URLEncoder.encode(url,"UTF-8");
    Log.d("TEST", encodedurl);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} 

Upvotes: 29

Related Questions