I am working on my very first Android application, I am getting some Arabic messages in the JSON response from a web service in two different formats. when I display one of them get translated correctly but other get printed as it is in the encoded message.
Here is the first one:
\u0635\u0641\u0631 \u0627\u0644\u0645\u0638\u0641\u0631
This is converted to proper Arabic string as intended.
but
کامران
does not, I was expecting that its a UTF-8 encoded message, but I'm unable to convert it. can anyone help me to understand this encoded message?
Here is how I tried to convert but its unchanged:
public String decodeString(String encodedString) {
try {
return new String(encodedString.getBytes(), "UTF-8");
} catch(Exception e){
e.printStackTrace();
return encodedString;
}
}
any help is appreciated.
Thank you very much for your time and assistance in this matter.
When I check (کامران) message here: http://www.cafewebmaster.com/online_tools/utf8_decode I get the correct response.
Please also share some details on the encoding scheme i-e what is the difference between both encodings.
کامران Looks like the HTML encoding of the Unicode code points. You'll need to decode the HTML, using e.g Apache commons StringEscapeUtils.unescapeHtml().
Here's the gradle dependency for the library:
compile 'commons-lang:commons-lang:2.6'
Related
I have an upstream server that accepts image submissions using rest. The submitted image is part of a JSON payload similar to this one
{
"name": "Blah.jpg",
"uploader": "user1",
"image": "<base64.....>"
}
Using this strategy works for small images but generates Out of Memory errors on larger images.
Is it possible to stream the base64 component of the image? Pass in something like an iterator that will be used to read chunks of the image, base64 them and send them directly to the network?
Not with Gson or Moshi. Both of these libraries require strings to be in memory before emitting them to a stream.
I solved this with the following, in a class that extends okhttp3.RequestBody:
private void writeFile(File file, BufferedSink sink) throws IOException {
byte buf[] = new byte[3000];
try (FileInputStream fin = new FileInputStream(file)) {
while (fin.read(buf) >= 0) {
byte encoded[] = Base64.encodeBase64(buf);
sink.write(encoded);
}
}
}
It uses Android's android.util.Base64 Apache Commons' org.apache.commons.codec.binary.Base64 to encode a buffered chunk of data.
I ended up writing the other json fields separately, with enough granularity that I could insert the file record exactly where I needed to.
EDIT:
As you can see in my edits above, I ended up switching to Apache commons-codec, via compile 'commons-codec:commons-codec:1.5' in my build.gradle file.
I didn't have time to investigate why the Android SDK solution didn't work. I tried their Base64.encode(buf, Base64.NO_WRAP) as suggested elsewhere - supposedly equivalent to Apache Commons' encodeBase64(byte[]) - but this did not work, hence the switch.
The problem could have been on our backend, so don't rule out Android SDK's solution based on my post alone - I just wanted to add this note so readers can see the code snippet that actually worked for me in the end.
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.
We have send a POST request to android web view , but the web view did some url decode automatically and the server side get a wrong data.
Eg: we have a signature value like aedTH5634+hjsGT78-67ty when we POST this value through webview , webview automatically convert + value to space.SO in the server signature value is wrong.How I Avoid this decode.
IOS webview workig fine it send exact value what we have POST.How we avoid this decoding from the android webview.
Help is highly appreciable,
Thanks,
not sure how it will help others with a similar situation but i will still give my $0.02, I was able to solve my issue like this, I used the following methods and ensured that the encoding is retained for encoding value use base64 and convert the data to base 64, read more about this here
void loadData (String data,
String mimeType,
String encoding)
convert data to base64 like this
String base64Data = android.util.Base64.encodeToString(yourdata.getBytes("UTF-8"), android.util.Base64.DEFAULT);
and finally put everything together like this
webView.loadData(base64Data, "text/html; charset=utf-8", "base64");
Maybe an Android WebView Bug?
Just a workaround:
webView.loadUrl(URLEncoder.encode(yourStr));
code works.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
this.evaluateJavascript(javascriptCommand, null);
} else {
this.loadUrl(javascriptCommand);
}
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.
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 "/".