本文共 1811 字,大约阅读时间需要 6 分钟。
package com.meeting.utils;import net.sf.json.JSONObject;import com.meeting.entity.weixin.WeixinUserInfo;/** * * @ClassName: WeixinUserInfoUtil * @Description: 获取微信用户的基本信息的工具类 * @author chengrui * @date 2017-3-28 上午10:26:51 */public class WeixinUserInfoUtil { /** * * @Title: getUserInfo * @Description: 获取微信用户基本信息 * @param openId * @return * @throws Exception */ public static WeixinUserInfo getUserInfo(String openId) throws Exception { WeixinUserInfo weixinUserInfo = null; // 请求的url和方法 String requestUrl = WeixinUtil.USERINFO_URL.replace("OPENID", openId); String requestMethod = WeixinUtil.GET; // 获取用户json信息 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, requestMethod, null); if (null != jsonObject) { try { weixinUserInfo = new WeixinUserInfo(); // 用户标识 weixinUserInfo.setOpenId(jsonObject.getString("openid")); // 关注状态 weixinUserInfo.setSubscribe(jsonObject.getInt("subscribe")); // 用户关注时间 weixinUserInfo.setSubscribeTime(jsonObject .getString("subscribe_time")); // 昵称 weixinUserInfo.setNickname(jsonObject.getString("nickname")); // 性别 weixinUserInfo.setSex(jsonObject.getInt("sex")); // 国家 weixinUserInfo.setCountry(jsonObject.getString("country")); // 省市 weixinUserInfo.setProvince(jsonObject.getString("province")); // 城市 weixinUserInfo.setCity(jsonObject.getString("city")); // 语言 weixinUserInfo.setLanguage(jsonObject.getString("language")); // 头像 weixinUserInfo .setHeadImgUrl(jsonObject.getString("headimgurl")); } catch (Exception e) { System.out.println("getUserInfo ERROR"); } } return weixinUserInfo; }}
转载地址:http://zuvin.baihongyu.com/