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
- Android Post Json数据乱码问题
- JSON数据乱码问题
- android post数据/问题
- httpclient post发送json数组并解决json乱码问题
- post、url发送数据乱码问题
- 接收post数据时汉字乱码问题
- java Post 发送数据乱码问题
- IOS开发 POST请求数据 乱码问题
- http post 接收数据 和 乱码问题
- 使用fetch post json数据的问题
- HTTPClient POST发送JSON数据 中文无乱码
- android 用json格式post数据
- android Json数据分装Post请求
- android post请求服务器出现乱码问题
- android 中post上传数据返回值是乱码问题的解决
- Android传统HTTP请求get----post方式提交数据(包含乱码问题)
- JSON数据的中文乱码问题
- getJSON获取JSON数据中文乱码问题
- 《转》详解Spring中bean的scope singleton prototype request...
- .config、kconfig和Makefile的关系
- 毛泽东23岁时写何文章震动全校?
- Javascript、Jquery获取浏览器和屏幕各种高度宽度
- 周期字串
- Android Post Json数据乱码问题
- 三维体数据分割算法及实现
- caffe运行错误 target_blobs.blobs_size()与 source_layer.blobs_size() 不一致
- 关于苹果开发证书失效的解决方案(2016年2月14日Failed to locate or generate matching signing assets)
- CI框架与Smarty模板整合
- 用C++11的std::async代替线程的创建
- 一月一本书,提升自己(2月-3月)--Java EE核心框架实战
- Java设计模式透析之 —— 策略(Strategy)
- 转移git仓库