Android Post Json数据乱码问题

来源:互联网 发布:mysql strcmp 编辑:程序博客网 时间:2024/04/30 15:39

这两天在做注册功能时,昵称里有汉字,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 0