手记

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

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

0人推荐
随时随地看视频
慕课网APP