android发送/接收Json包含中文的处理

转自:http://wiki.neal365.com/2013/02/25/android%E5%8F%91%E9%80%81%E6%8E%A5%E6%94%B6json%E5%8C%85%E5%90%AB%E4%B8%AD%E6%96%87%E7%9A%84%E5%A4%84%E7%90%86/

问题:

在Android向Python实现的Wsgi接口发送json格式的http请求是,server不能正确解析,入库的中文字段为一串问号???????,在server端反复解码无效,查找资料,是在Android发送json时没有指定utf8,指定后,一切OK。

即在发送的时候用utf-8编码,接收的时候用utf-8解码即可!

HttpPost httpPost = new HttpPost(url);

StringEntity se = new StringEntity(jsonObject.toString(), “UTF-8);
httpPost.setEntity(se);
se.setContentType(“application/json”);

HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
String retSrc = EntityUtils.toString(httpResponse.getEntity(), “UTF-8);
JSONObject result = new JSONObject( retSrc);