Json.org Android adds weird characters

I have a client / server application written using Android and I use the standard org.json package classes bundled with Android to parse and create.

I get weird characters appearing on the server side right in the middle of the json lines generated, for example (not complete, because it's big):

{!lo":"es_MX","id":2791884,"os":"8"} 

As you can see (!) The exclamation mark appears randomly instead of a double quote. I also get other random characters appearing in the middle of the line. It is very strange.

Here is the code that creates the JSON object ...

 JSONObject jsonObject = new JSONObject(); jsonObject.put("key", someValue); 

Here is the code that sends.

 HttpPost type = new HttpPost(<server url here>); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("v", jsonObject.toString())); type.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpClient.execute(type); // This is a DefaultHttpClient 

I say a random but exclamation point in this exact position is consistent in many mistakes, but not every time. About 5 messages that receive this error are among tens of thousands per day. And usually this is not the contents of the values ​​inserted in json, but the characters (such as the character above) that define the structure of the message, which tells me that this is not a character set problem.

Has anyone come across this?

+4
source share
5 answers

It seems you are composing a string in a different format and when receiving text decoding in a different format like iso to utf.

0
source

It looks like your sender is incorrectly setting the character set. The Spaniards will have characters not found in regular ASCII or most Windows encodings, you need to use UTF-8:

Content-Type: text/html; charset=utf-8

Not knowing which HTTP exchange you are using ( read more ), it is impossible to give you the exact code snippet to fix the problem - but it should be easy enough to understand.

0
source

You are not providing enough information. A radical way to fix your problem is to simply replace all the characters (!) With (").

 string.replaceAll("!", "\""); 

I assume this is a server side issue.

0
source

I also had a similar problem. Let me write a lot more to describe my environment. My server was returning data in json format. But my problem was with special characters like ąść . You should know json_encode() will return the string text as null from the server in this case. I know this sucks! So I added mysql_query('SET CHARACTER SET utf8'); before doing selction for items from the database. This allowed me to take lines from the server with special diacritical letters.

Now on the application site I received data from the server using the GET method. At first, I stored the result data in an InputStream . Then I packed it in InputStreamReader, and byte by byte added it to stringBuilder. This finished text was added to the string toString() ready. Then I put it on new JsonArray(readyString) . However, I found that some parts of the text for json had strange characters. Especially in those places where there were special letters such as żóć . For example, "description":"aaa" throws out "descriptionPffa": "aa" null: `.

I decided to try another way to convert the result from data. In the places where I converted the data from the server, I used the method below. In the end, wgen I got a byteArrayOutputStream object, I changed it to new String(byteArray) , and then somehow it worked with new JsonArray(new String(byteArray)) !

 public class Streams { public static byte[] getBytes(InputStream is) { int len; int size = 1024; byte[] buf = new byte[0]; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); buf = new byte[size]; while ((len = is.read(buf, 0, size)) != -1) bos.write(buf, 0, len); buf = bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return buf; } } 
0
source

Print json on the client (using Log.d or similar) and see if it contains strange characters before sending it to the server.

-one
source

Source: https://habr.com/ru/post/1393038/


All Articles