Android Post Json数据乱码问题

这两天在做注册功能时,昵称里有汉字,tomcat服务器后台日志及数据库的汉字都显示?问号。服务器是CentOS 7.0, 将系统语言配置成 LANG=zh_CN.UTF-8,服务器端代码的web.xml中已经添加了org.springframework.web.filter.CharacterEncodingFilter的Filter。结果还是乱码,问题可能出在Android客户端上。网上查了些资料,并尝试了更改不同位置的代码,定位到了问题。


Json数据如下格式:

{"phone":"18688888888","passwd":"123456","nickname":"汉字","verifycode":"2769"}


private static final String APPLICATION_JSON = "application/json;charset=utf-8";

public static <T extends Result> T postJson(String url, String postBodyJson, Class<T> clazz) throws IOException {
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(postBodyJson)) {
            Log.e(TAG, "url/postBodyJson should not be null");
            return null;
        }

	DefaultHttpClient httpClient = getHttpClient();
	httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, TIMEOUT);
        httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, TIMEOUT);

        HttpPost httpPost = new HttpPost(url);

        StringEntity stringEntity = new StringEntity(postBodyJson);
        stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
        httpPost.setEntity(stringEntity);

        httpPost.setHeader(HTTP.USER_AGENT, "xxx/1.0");
        httpPost.setHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
        httpPost.setHeader("Accept", APPLICATION_JSON);

        PersistentCookieStore cookieStore = MyFragment.getInstance().getCookieStore();
        httpClient.setCookieStore(cookieStore);

        HttpResponse response = httpClient.execute(httpPost);
        int code = response.getStatusLine().getStatusCode();

        if (HttpStatus.SC_OK == code) {

            //Login succeed
            if (StringUtils.isNotEmpty(url) && url.contains(Config.REQUEST_URL_LOGIN)) {
                //Save cookies
                List<Cookie> cookies = httpClient.getCookieStore().getCookies();

                for (Cookie cookie : cookies) {
                    cookieStore.addCookie(cookie);
                }
            }

            String rspStr = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            Log.d(TAG, rspStr);

            T result = null;
            try {
                result = JSONHelper.parseObject(rspStr, clazz);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return result;

        } else if (HttpStatus.SC_UNAUTHORIZED == code) {
	    throw new UnauthorizedException("Unauthorized. Need to login again.");
	} else {
            throw new IOException("ERROR: rspCode=" + code + "-" + response.getEntity().toString());
        }
}

尝试了增加声明utf-8的header和stringEntity.setContentEncoding之后,问题依旧。


其中,如下一行

StringEntity stringEntity = new StringEntity(postBodyJson);

增加UTF-8编码后,再测试数据库和tomcat log已经变成了汉字,问题解决。

StringEntity stringEntity = new StringEntity(postBodyJson, "UTF-8"); //不带UTF-8,汉字会是乱码

参考:

http://blog.csdn.net/bonnshore/article/details/18327881

http://www.jb51.net/article/49132.htm

http://chaico.iteye.com/blog/1954128





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值