微信公眾號開發--獲取用戶信息中文亂碼的解決方案
阿新 • • 發佈:2017-05-26
其中 utf-8 == font zzu 解決 col class api
在微信開發中我們會經常需要獲取用戶的信息。
微信給我們提供了獲取用戶信息的api,
地址為
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
將其中的access_token替換為我們的access_token
openid為關註用戶的openid就可以獲取到用戶的信息了。
問題描述
不過獲取到的數據卻是中文亂碼
而用Java程序獲取的結果也是一樣的
解決方案
這個亂碼主要是由於微信那邊采用的是“ISO-8859-1”編碼造成的。
我們對獲取的結果進行編碼
String token = AccessTokenTool.getAccessToken(); String URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; // 原始json String jsonResult = HttpUtil.sendGet(URL.replace("OPENID", openid).replace("ACCESS_TOKEN", token)); System.out.println(jsonResult);// 編碼後的json String json = new String(jsonResult.getBytes("ISO-8859-1"), "UTF-8"); System.out.println(json);
獲取自定義菜單亂碼也是一樣的解決方法
參考文獻
獲取用戶基本信息(UnionID機制)
HttpUtil工具類參考
http://blog.csdn.net/frankcheng5143/article/details/51612550
微信公眾號開發--獲取用戶信息中文亂碼的解決方案