Garbled characters occur when retrieving user information.
When binding a user, you need to obtain the user information, which is prone to garbled characters.
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);
Set String result =Method. getResponseBodyAsString ();
Change to String result =New String (method. getResponseBody (), "UTF-8");
You can.
Method. getResponseBodyAsString ():
Returns the response body of the HTTP method, if any, asString
. If response body is not available or cannot be read,NullIs returned. The raw bytes in the body are converted toString
Using the character encoding specified in the response'sContent-TypeHeader, 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,null
Will be returned.
New String (method. getResponseBody (), "UTF-8 ")
Encode the native bytes returned by method. getResponseBody () with the specified UTF-8 encoding into a String. The returned value is UTF-8 encoding.
Running result:
No garbled characters appear.