In my app I receive a URL such as
http://www.wassersportlotse.de/php/lib/smart_image_resizer/image.php/Mühlendammschleuse.jpg?image=/media/images/uploads/Mühlendammschleuse.jpg
When there are no German characters in the fullurl I can just use it without encoding and it works fine. However if I receive a URL such as the one above it doesn't work (the ü is causing the problem). Below I have tried to encode the seperate parts of the URI to no avail. As alway advice is very much appreciated.
public ImageDownloader(String fullurl) throws URISyntaxException{
URI uri = new URI(fullurl);
path = uri.getPath();
path = URLEncoder.encode(path);
query = uri.getQuery();
query = URLEncoder.encode(query);
auth = uri.getAuthority();
url = "http://" + auth + path + query;
}
Maybe the encoder das encode the Umlaut as UTF-8 characters (so ü would be encoded with two characters) and they are not put back together properly at the server (for us it didn't work with Tomcat). To solve this situation we used URLEncoder.encode(param, "ISO-8859-1") to encode the parameters.
There's no simple answer, because it depends on the server serving that URI which encoding is expected.
Usually it's UTF-8.
In that case: use String.getBytes, specifying the UTF-8 encoding, and obtain a byte array from that. Re-encode that byte array as string by taking all bytes <= 127 as-is, and substituting all others by the %hh form. (percent sign, then two hex digits). See http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.2.1.
You can use Android's Uri class to help you out. That class has an encode() method which will use UTF-8 to encode your string.
I recently had a problem with URLs for images whose names included umlauts and German special characters, and I lost a day looking for the solution. The images simply did not appear if there was an ä or and ü in the file name or the directory name. I thought it might be spring, or some other Java technology I am working with, or in the browser. And strangely enough, even with the url encoded, it failed to find the image. But in the end, the solution was in my tomcat server.xml configuration. In your server.xml file, find your connector and add these two lines:
URIEncoding="UTF-8"
useBodyEncodingForURI="true"
At the end, it should look something like this:
<Connector connectionTimeout="20000"
port="8080"
protocol="HTTP/1.1"
redirectPort="8443"
URIEncoding="UTF-8"
useBodyEncodingForURI="true"/>
Now I do not need to url-encode the url. This is a help to my clients, because they can see the German words in the urls spelled correctly.
Here is another tip: if you are coding in eclipse and starting and stopping your server from inside eclipse, then the configuration file (server.xml) could be in your eclipse workspace in the Servers folder. It must be changed here for it to work with eclipse. This can be maddening, when you have made the change in your principal tomcat configuration, and the urls work there, but they are still broken when running the server in eclipse.
That did it for me. I hope it helps someone out there! :-)
Have your tried unsing:
android.net.Uri.encode(urlString, ":/");
It encodes the string but skips ":" and "/".
Related
I am calling a web service through async task which is returning text in strange format. here is sample string
dhmot_enot = Ï. ÎÎ®Î¼Î¿Ï ÎοÏλαÏ
zoe_name = Î.Î.Î: ÎÏÎ½ÎµÏ Î ÏοÏÏαÏÎ¯Î±Ï ÎÏοÏÏ Î¥Î¼Î·ÏÏοÏ
zones_zoe = ÎΩÎÎ Î: ÎÎ ÎÎΥΤΠΠΡÎΣΤÎΣÎΠΤÎΣ ΦΥΣÎΣ
zoe_fek = 187/Î/2011
fek_rel = 544/Î/1978
yphresia = Î¥.ÎÎÎ Î. ÎάÏηÏ-ÎοÏλαÏ-ÎοÏλιαγμÎνηÏ
How to find and resolve this ?
Update 1
Here is the actual service link that i am calling from server (works well in web browser ) but when i call from android it looks like above
http://geo-polis.gistemp.com/geoserver/wms?service=WMS&version=1.1.1&srs=EPSG:4326&bbox=23.733829893171787,37.75098946973509,23.839769437909126,37.89294194933182&styles=&&buffer=20&OUTPUTFORMAT=json&request=GetFeatureInfo&layers=geopolis:oria_eniaiou_dhmou&query_layers=geopolis:oria_eniaiou_dhmou&width=1080&height=1832&x=690&y=821
The response is a normal UTF-8 encoded stream of data. To see this, go to the URL you show in your post in the browser, and look at the encoding it picked automatically: it'll show unicode/utf-8 as character encoding for the response. If you change that, forcing the browser to decode it as if it's ANSI encoded (windows codepage 1252/ISO-8859-15) then the text turns into the gibberish you were showing in your question, so: you're not decoding the data correctly, and need to make sure to decode as utf8.
I am working with special character in URL for android
However, I encounter two problem
1) empty space
If I have a query inside url
e.g. test.php?test=aaa bbbb cccc
Then the query will not include bbbb and cccc, I learnt that I should replace the " " to %20, however, instead of using replace(" ","%20"), how can I do it in more standard way?
2) traditional chinese in url
I have an image url like this:
http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-職安健資訊產品目錄2013-220x300.png
If I directly pass to android, it fail. but if I copy the link to my desktop browser , it change to like this, then I paste it on android, it works
"http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-%E8%81%B7%E5%AE%89%E5%81%A5%E8%B3%87%E8%A8%8A%E7%94%A2%E5%93%81%E7%9B%AE%E9%8C%842013-220x300.png";
What should I do to encode to this?
Thanks for helping
Update:
it change to
http%3A%2F%2Foshc.zizsoft.com%2Fwp-content%2Fuploads%2F2013%2F12%2FSQ1-351x300.jpg
How can I fix that? Thanks
1) Try trimming the url, it wil remove the spaces from url. I guess its bit more staanddard way to solve this issue.
2) This problem is eactly due to the encoding issues. Our Android default encoding is cp1252...and those strings will not be encoded in the same way so when it used in the code its automatically changes to some other symbols. So try changing the encoding of the project and string to the same like UTF-8 or something. You can change project encoding by
Properties> Resources> Encoding
Hope my suggesions will help you a bit.
Try this links also
1. Trimming
2. Encoding
If you want to programmatically encode and decode URLs then use URLEncoder and URLDecoder class available in Java as well as Android.
// To encode URL
URLEncoder.encode(url, charset);
// to decode url
URLDecoder.decode(url, charset);
I am having a curious problem that perhaps someone has insight into. I encode a query string into a URL on Android using the following code:
request = REQUEST_BASE + "?action=loadauthor&author=" + URLEncoder.encode(author, "UTF-8");
I then add a few other parameters to the string and create a URI like this:
uri = new URI(request);
At a certain point, I pull out the query string to make a checksum:
uri.getRawQuery().getBytes();
Then I send it on its way with:
HttpGet get = new HttpGet(uri);
On the Appengine server, I then retrieve the string and try to match the checksum:
String query = req.getQueryString();
Normally, this works fine. However, there are a few characters that seem to get unencoded on the way to the server. For example,
action=loadauthor&author=Charles+Alexander+%28Ohiyesa%29+Eastman×tamp=1343261225838&user=1479845600
shows up in the server logs (and in the GAE app) as:
action=loadauthor&author=Charles+Alexander+(Ohiyesa)+Eastman×tamp=1343261226837&user=1479845600
This only happens to a few characters (like parentheses). Other characters remain encoded all the way through. Does anyone have a thought about what I might be doing wrong? Any feedback is appreciated.
I never did find a solution for this problem. I worked around it by unencoding certain characters on the client before sending things to the server:
request = request.replace("%28", "(");
request = request.replace("%29", ")");
request = request.replace("%27", "'");
If anyone has a better solution, I am sure that I (and others) would be interested!
URLEncoder does not encode parentheses and certain other characters, as they are supposed to be "safe" for most servers. See URLEncoder. You will have to replace these yourself if necessary.
Example:
URI uri = new URI(request.replace("(","%28"));
If a lot of replacements are needed, you can try request.replaceAll(String regularExpression, String replacement). This, of course, requires knowledge of regular expressions.
I'm having a slight problem opening a certain URL in the browser. First of all I use the following code to launch the browser:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Globals.currentChatURL));
startActivity(Intent.createChooser(browserIntent, "Select browser:"));
Now if I set Globals.currentChatURL to something like http://www.google.com then it opens that site just fine. But my URL is a little more complicated as it contains multiple parameters which are all base64 encoded. Here is an example of how my URL looks:
http://webportal.mysite.com/ChatProgram/chat.php? intgroup=UFYyMA==&intid=UFYyMEZN&hg=Pw__&pref=user&en=U0NPVFQgTUlMTEFS&ee=cGF1bGdAbWFnbmF0ZWNoLmNvbQ==&eq=UFRWRkVI&ec=TUFHTkFURUNI
Now if I use my above code to try and launch this URL it brings me to the Google search page with the following message:
"Your search - http://URLabove ... did not match any documents"
Yet if I copy the URL and paste it into the address box it brings me to the right place. How can I fix this?? The whole point of this is to have the user click the button and the site to launch, not for the user to have to copy and paste the URL manually.
Any suggestions would be greatly appreciated.
Thanks a lot
There is unwanted equal signs in the query part of your http URI. Such signs have a specific meaning as delimiters in the form ¶meter=value.
This equal signs represents padding values (0, 1 or 2) from your base64 encoding.
You can either
remove them because your base64 server decoder won't bother reconstructing them, or
percent encode them (with all other reserved characters).
In android you can use percent encode this way:
String value = URLEncoder.encode("annoying values with reserved chars &=#", "utf-8");
String url = "http://stackoverflow.com/search?q=" + value;
The RFC 2396 is now deprecated but that is what URI.parse() is based on as stated by the documentation:
uriString an RFC 2396-compliant, encoded URI
An exception is thrown when I run this code. If you replace the Hindi characters in the URL with "Hello" it plays the file just fine.
When I load this URL (with the Hindi characters) in a browser it plays just fine.
What's going on?
Here's my code:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getResources().getString(R.string.test));
mediaPlayer.prepare();
Here's the string resource def:
<string name="test">http://translate.google.com/translate_tts?q=आलू</string>
I don't think unicode characters are legal in URLs, unless you encode them. Here's the spec:
https://www.rfc-editor.org/rfc/rfc1738
+1 tdammers is right, you can't have non-ASCII characters in a URI.
You can have them in an IRI, which is what this is:
http://translate.google.com/translate_tts?q=आलू
Browsers typically support IRIs (with some limitations), but many other tools don't (including, apparently, the Android media player). For those tools, you have to convert the IRI to a URI. This is done by:
taking any non-ASCII characters in the hostname part of the address and encoding them using the IDN algorithm;
taking any non-ASCII characters in other parts of the address (like here, the query) and %-encoding their UTF-8 byte representation.
This gives you:
http://translate.google.com/translate_tts?q=%e0%a4%86%e0%a4%b2%e0%a5%82
which should work anywhere. (And paste a URI like this into a browser and typically it'll display it in IRI form with the Hindi in the address bar.)
I have found the solution from other person. You have encode the text first. Then you have to add the encode method in "ie" parameter. For your text if you set the url for mediaplayer as http://translate.google.com/translate_tts?ie=UTF-8&q=%e0%a4%86%e0%a4%b2%e0%a5%82 it will play the desired word.