获取微信用户信息出现乱码
2015-09-04 21:53
711 查看
在绑定用户微信时,需要从微信获取用户信息,此处容易出现乱码。
将 String result = method.getResponseBodyAsString();
换成 String result = new String(method.getResponseBody(), "utf-8");
即可。
method.getResponseBodyAsString():
Returns the response body of the HTTP method, if any, as a
Note that this method does not propagate I/O exceptions. If an error occurs while reading the body,
而:new String(method.getResponseBody(), "utf-8")
将 method.getResponseBody() 返回的原生字节用指定的 utf-8 编码,编码成String。因为微信的返回值就是采用的utf-8编码。
运行结果:
![](http://images2015.cnblogs.com/blog/699877/201509/699877-20150904215117045-215652738.png)
没有乱码出现。
JSONObject jo = this.getAccessTokenOpenid(code); StringBuilder sb = new StringBuilder("https://api.weixin.qq.com/sns/userinfo?access_token="); sb.append(jo.getString("access_token")); sb.append("&openid=").append(jo.getString("openid")).append("&lang=zh_CN"); HttpMethod method = new PostMethod(sb.toString()); HttpClient httpclient = new HttpClient(); httpclient.executeMethod(method); String result = new String(method.getResponseBody(), "utf-8"); // String result = method.getResponseBodyAsString(); System.out.println("getWeiXinUserInfo result = " + result); JSONObject userInfo = JSON.parseObject(result, JSONObject.class);
将 String result = method.getResponseBodyAsString();
换成 String result = new String(method.getResponseBody(), "utf-8");
即可。
method.getResponseBodyAsString():
Returns the response body of the HTTP method, if any, as a
String. If response body is not available or cannot be read, null is returned. The raw bytes in the body are converted to a
Stringusing the character encoding specified in the response's Content-Type header, or ISO-8859-1 if the response did not specify a character set.
Note that this method does not propagate I/O exceptions. If an error occurs while reading the body,
nullwill be returned.
而:new String(method.getResponseBody(), "utf-8")
将 method.getResponseBody() 返回的原生字节用指定的 utf-8 编码,编码成String。因为微信的返回值就是采用的utf-8编码。
运行结果:
![](http://images2015.cnblogs.com/blog/699877/201509/699877-20150904215117045-215652738.png)
没有乱码出现。
相关文章推荐
- 平时编些小程序
- 微信支付 php jsapi 配置调试
- android---仿微信主界面设计
- 基于第三方微信授权登录的iOS代码分析
- 开源分享 一(StickerCamera + 仿微信多图选择)
- [置顶] 微信sdk应用消息的接入(android)
- SwipeListView 详解 实现微信,QQ等滑动删除效果
- [微信营销企划之路]003.Access forbidden!
- 微信公众平台开发教程(十一)微信"企业号“上线
- 微信公众平台开发教程(十) 订阅号与服务号的区别
- 微信公众平台开发教程(九)微信公众平台通用开发框架
- 微信公众平台开发教程(八)Session处理
- 微信公众平台开发教程(七)安全策略
- 微信公众平台开发教程(六)获取个性二维码
- 微信公众平台开发教程(五)自定义菜单
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- 微信公众平台开发教程(三) 基础框架搭建
- 微信公众平台开发教程(二) 基本原理及消息接口
- 微信公众平台开发教程(一) 微信公众账号注册流程
- 八大金刚齐上阵 微信硬件能解决商业化症结?