继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

微信登录获取用户信息 - 避免用户昵称乱码

吃鸡游戏
关注TA
已关注
手记 297
粉丝 55
获赞 339

String userInfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid;

// access_token openid 使用code请求链接后获取到的

stringRequest = new StringRequest(Request.Method.GET, userInfo_url, new Response.Listener() {

  @Override 

  public void onResponse(String response) { 

 Log.i("获取WX用户个人信息","************************ 输出数据 response = "+response); 

 JSONObject jsonObject = JSON.parseObject(response); 

 try {

String headimgurl = jsonObject.get("headimgurl").toString(); 

String openid = jsonObject.get("openid").toString();

String unionid = jsonObject.get("unionid").toString(); 

String nickname = jsonObject.get("nickname").toString();

// 这个地方转码一下就ok了

 try {

 nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8"); 

 Log.i("打印微信个人用户数据", "******************* nickname = " + nickname); 

  } catch (UnsupportedEncodingException e) {

 e.printStackTrace(); 

  }

  } catch (Exception e) { 

  Log.i("获取WX用户个人信息", "******************* 解析失败 Exception e = " + e.toString());

  } 

 }

}, new Response.ErrorListener() { 

 @Override 

 public void onErrorResponse(VolleyError error) {

 // 请求错误就会进入这里 

 Log.i("获取WX用户个人信息", "******************* 请求失败 error = " + error.toString()); 

}

});requestQueue.add(stringRequest);

原文链接:http://www.apkbus.com/blog-722618-77952.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP