khaintt / android-json-rpc

Automatically exported from code.google.com/p/android-json-rpc
0 stars 0 forks source link

Can not work with json-string which contains non-ascii unicode characters. #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. new a JSONObject and put a string field
JSONObject jsonObject = new JSONObject();
jsonObject.put("string": "中文");
2. New a json rpc client:
JSONRPCClient client = ...
3. Use the jsonObject as json-rpc parameter:
client.callJSONObject("method": jsonObject);

What is the expected output? What do you see instead?

The remote side receives '????' instead of "中文"

What version of the product are you using? On what operating system?

Working with Android-7 and android-jsonrpc-0.3.1

Please provide any additional information below.

Can be fixed by changing lines in org.alexd.jsonrpc.JSONEntity:

package org.alexd.jsonrpc;

import java.io.UnsupportedEncodingException;

import org.apache.http.Header;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

/**
 * Provides a HttpEntity for json content
 */
class JSONEntity extends StringEntity 
{
    public JSONEntity(JSONObject jsonObject) throws UnsupportedEncodingException 
    {
        super(jsonObject.toString(), "utf-8");
        setContentEncoding("utf-8");
    }

    @Override
    public Header getContentType() 
    {
        return new BasicHeader(HTTP.CONTENT_TYPE, "application/json");
    }
}

Original issue reported on code.google.com by lvqier on 26 Jun 2012 at 2:12

GoogleCodeExporter commented 9 years ago
I'll handle this one.

Original comment by Spd...@gmail.com on 29 Jun 2012 at 5:43

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Issue Fixed with version 0.3.3.

Please control and report.

SpdyMx

Original comment by Spd...@gmail.com on 29 Jun 2012 at 10:28

GoogleCodeExporter commented 9 years ago
The default encoding should be UTF-8 since the RFC-4627 
(http://www.ietf.org/rfc/rfc4627.txt) says:

3.  Encoding

   JSON text SHALL be encoded in Unicode.  The default encoding is
   UTF-8.

   Since the first two characters of a JSON text will always be ASCII
   characters [RFC0020], it is possible to determine whether an octet
   stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
   at the pattern of nulls in the first four octets.

           00 00 00 xx  UTF-32BE
           00 xx 00 xx  UTF-16BE
           xx 00 00 00  UTF-32LE
           xx 00 xx 00  UTF-16LE
           xx xx xx xx  UTF-8

So what about change the 15th line in org.alexd.jsonrpc.JSONRPCClient.java from:

protected String encoding = "";

to:

protected String encoding = HTTP.UTF_8;

Thanks.
lvqier

Original comment by lvqier on 29 Jun 2012 at 5:14