Android Post Json数据乱码问题
2016-02-19 13:55
477 查看
这两天在做注册功能时,昵称里有汉字,tomcat服务器后台日志及数据库的汉字都显示?问号。服务器是CentOS 7.0, 将系统语言配置成 LANG=zh_CN.UTF-8,服务器端代码的web.xml中已经添加了org.springframework.web.filter.CharacterEncodingFilter的Filter。结果还是乱码,问题可能出在Android客户端上。网上查了些资料,并尝试了更改不同位置的代码,定位到了问题。
Json数据如下格式:
尝试了增加声明utf-8的header和stringEntity.setContentEncoding之后,问题依旧。
其中,如下一行
增加UTF-8编码后,再测试数据库和tomcat log已经变成了汉字,问题解决。
参考:
http://blog.csdn.net/bonnshore/article/details/18327881 http://www.jb51.net/article/49132.htm http://chaico.iteye.com/blog/1954128
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
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories