Reputation: 10777
I have a link that has a space in it, but when i paste it to the browser, it loads the page and the link changes slightly, here is the link:
http://www.marketimyilmazlar.com/image/cache/data/DUZELTME/dvm/screenshot 7-500x500.jpg
But it returns into the following when browser loas the page:
http://www.marketimyilmazlar.com/image/cache/data/DUZELTME/dvm/screenshot%207-500x500.jpg
Here is what i do in java:
public static Drawable LoadImageFromWeb(String link) throws URISyntaxException {
try {
URI uri = new URI(link);
URL url = new URL(uri.toASCIIString());
InputStream is = (InputStream) url.getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (IOException e) {
Log.e("HATA", "THERE is an error", e);
return null;
}
}
But the problem is, I get the following exception:
java.net.URISyntaxException: Illegal character in path at index 73:
So, can anyone help me with this? Thanks
Upvotes: 1
Views: 1881
Reputation: 883
Doing proper URL parsing with Java standard library is far from trivial. I grew frustrated when hitting edge cases. So I developed a library for proper URL parsing in Java: galimatias. Any URL that works in a web browser should work with galimatias.
With io.mola.galimatias.URL.parse(link)
you get a io.mola.galimatias.URL
object. Here parsing will work with almost whatever you throw at it (unless it can't really be interpreted as a URL). Then you can transform it to a Java URL with toJavaURL()
.
For your use case, you would write:
public static Drawable LoadImageFromWeb(String link) {
try {
URL url = io.mola.galimatias.URL.parse(link).toJavaURL();
InputStream is = (InputStream) url.getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (IOException e) {
Log.e("HATA", "THERE is an error", e);
return null;
} catch (GalimatiasParseError e) {
Log.e("HATA", "THERE is an error", e);
return null;
} catch (MalformedURLException e) {
Log.e("HATA", "THERE is an error", e);
return null;
}
}
Upvotes: 2
Reputation: 122364
The java.net.URI
class may be able to help you here - its single argument constructor expects the argument to be a proper URI with illegal characters escaped, but its multiple-argument constructors can handle un-escaped parts. If your initial URL does not contain any %-escapes already, and does not have a #
fragment on the end then you can do
String escaped = new URI("dummy", unescaped, null).getRawSchemeSpecificPart();
The trick here is that the three-argument constructor expects a "scheme", a "scheme-specific part" and a "fragment", all in unescaped form. So for your example in the question
new URI("dummy", unescaped, null)
will give you the properly-escaped URI
dummy:http://www.marketimyilmazlar.com/image/cache/data/DUZELTME/dvm/screenshot%207-500x500.jpg
Extracting the raw SSP from that URI will give you everything after the first colon, i.e.
http://www.marketimyilmazlar.com/image/cache/data/DUZELTME/dvm/screenshot%207-500x500.jpg
Upvotes: 7
Reputation: 6017
The URL must be encoded, and Java has an object for that. Use URLEncode.encode()
on your string.
Upvotes: -2