Unicode URL encode / decode with Python - android

I have an Android app which uses
URLEncoder.encode(S.getSongArtist(),"UTF-8")
to encode a unicode string that is posted to a AppEngine python (2.7) web service. On the service I use
urllib.unquote_plus(artist)
This is not giving me correct results. I have an input like this:
Marie+Lafor%C3%AAt
which is unquote'd to
Marie Laforêt
If I use a javascript url decode, for instance: http://meyerweb.com/eric/tools/dencoder/
I get
Marie Laforêt
A correct result.
I tried using
urllib.unquote(artist).decode('utf-8')
but this generates an exception. Any hints at all are greatly appreciated.
EDIT
Taxellool had the right answer in the comments:
what you are trying to decode is already decoded. try this:
urllib.unquote_plus(artist.encode('utf-8')).decode('utf-8')

Taxellool had the right answer in the comments:
what you are trying to decode is already decoded. try this:
urllib.unquote_plus(artist.encode('utf-8')).decode('utf-8')

I guess you are decoding before urllib.unquote():
>>> print urllib.unquote_plus('Marie+Lafor%C3%AAt'.decode('utf-8'))
Marie Laforêt
If you decode after unquote, result would be what you want:
>>> print urllib.unquote_plus('Marie+Lafor%C3%AAt').decode('utf-8')
Marie Laforêt
Just make sure you don't pass a unicode to urllib.unquote_plus.

Related

Android string font is some different format/font

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.

Replace some special characters with browser code

I am facing a strange situation here.
I am parsing an image link through json and getting the url:
http://developersatwork.com/projects/g4/tools/wp-content/uploads/2015/05/Belbin’s-Team-Roles-100x100.jpg
Whenever I paste this in browser then the image is showing,but whenever I paste it in a notepad then the following url is showing up:
http://developersatwork.com/projects/g4/tools/wp-content/uploads/2015/05/Belbin%E2%80%99s-Team-Roles-100x100.jpg
As you can apostrophe is replaced by %E2%80%99 and Json cannot detect the
apostrophe and as a result it is throwing exception:
06-15 14:01:16.955: W/System.err(10146): java.io.FileNotFoundException: http://developersatwork.com/projects/g4/tools/wp-content/uploads/2015/05/Belbin’s-Team-Roles-100x100.jpg
How can I replace such special characters with their corresponding code so that Json can detect them??
I tried UTF-8 encoding and decoding but it is not working.Please give a generalised solution but not for a specific character.
Another one:
http://www.tools4management.dreamhosters.com/wp-content/uploads/2015/06/The-POSITIVE-Model-of-Coaching-–-An-8-step-Outline-to-Motivate-Goal-Accomplishment-100x100.jpg
But when I paste the link from browser to a notepad it shows:
http://www.tools4management.dreamhosters.com/wp-content/uploads/2015/06/The-POSITIVE-Model-of-Coaching-%E2%80%93-An-8-step-Outline-to-Motivate-Goal-Accomplishment-100x100.jpg
SOLUTION
The only workaround was to use a Webview instead of ImageView.
You need to decode the url.
url = java.net.URLDecoder.decode(url, "UTF-8");

Work with the link with special character (e.g. traditional chinese , empty space) in android

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);

Encoding URL with German Characters

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 "/".

get image from remote server

i have image in sqlserver db.using ksoap2 client webservice for getting image.there i am converting image into base64 encoding and in mobile i am converting it into base64 decoding but i am unable to get image
whats the problem.how i will get image or any alternative solution for this problem
Thanks in advance
Aswan
finally i got solution for my problem by creating custom base64 class
If you decode from base64 what you get should be technically an image file and cannot be used directly in the code.
Maybe what you are looking for is something like
Image i = BitmapFactory.decodeByteArray(data, offset, length);
Where "data" is a byte array you have after the decoding, offset should be set to 0 and length equal to data.length.
After that, you can use the image with your views.

Categories

Resources