17

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

Molly
  • 1,887
  • 3
  • 17
  • 34
sivaraj
  • 1,849
  • 9
  • 35
  • 52
  • An URL with a white space instead of %20 is an illegal URL. So you need a parser for broken urls (unless you can change them manually before parsing). – LatinSuD Sep 17 '10 at 11:34
  • 1
    Possible duplicate of [Url encoding in Android](http://stackoverflow.com/questions/3286067/url-encoding-in-android) – blahdiblah Apr 14 '12 at 00:06

8 Answers8

29

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();
} 
Cpt.Ohlund
  • 2,589
  • 1
  • 20
  • 15
  • @http://www.theblacksheeponline.com/party_img/thumbspps/12390867930_15951_186997180114_709920114_4296270_6115611_n[1].jpg How can i encode this kind of url?please give sample code for me.. – sivaraj Sep 20 '10 at 12:46
  • 42
    That example outputs the URL: http%3A%2F%2Fwww.example.com%2F%3Fid%3D123%26art%3Dabc . That's not a valid URL at all. The URLEncoder.encode method is for encoding query string values rather than whole URLs. Quote from docs: 'This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format' – Ben Clayton Feb 28 '11 at 19:02
  • 4
    That's not a valid encoding. Look at the answer here: http://stackoverflow.com/questions/3286067/url-encoding-in-android – Sagar Hatekar Mar 14 '12 at 14:25
  • It would be nice to fix your example... or delete it. – ben75 Jun 09 '15 at 14:27
  • How to encode this url to be possible to share and then user be able to open the link and see the page : http://yazd20.com//News/2015/11/استند-آب-كمدي-حسن-ريوندي-در-يزد.html – Ahmad Ebrahimi Nov 02 '15 at 21:21
  • thanx alot.. it worked, but it shows `20%` instead of spaces, so I had to replace it with normal spaces like this > `URLEncoder.encode(note,"UTF-8").trim().replace(" ", "%20")` – Alaa AbuZarifa Jun 08 '17 at 07:41
16

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

Jedo
  • 811
  • 7
  • 4
6

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.

BitMask777
  • 2,543
  • 26
  • 36
4

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;
Dhruv Raval
  • 4,946
  • 3
  • 29
  • 34
1

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();
Oleg Smirnov
  • 106
  • 3
1

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();
}
Elango
  • 71
  • 2
1

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.

Krunal Shah
  • 1,438
  • 1
  • 17
  • 29
0

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

Daniel Murphy
  • 852
  • 7
  • 14