获取openId需要用到access_token,上一篇有获取access_token的方法
获取access_token
public class GetOpenId {
public static void main(String[] args) throws Exception {
String[] openIds = getOpenId();
getUserInfo(openIds);
}
private static String OPEN_ID_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
public static String getOprnId_url() throws Exception{
return OPEN_ID_URL.replace("ACCESS_TOKEN",GetAccessToken.getAccessToken()).replace("NEXT_OPENID","");
}
/**
* 获取openId
* @return
* @throws Exception
*/
public static String[] getOpenId() throws Exception {
//获取token
String openId = null;
String openIdJson = WeiXinUtlis.sendGet(getOprnId_url());
String openIds = JSONObject.fromObject(openIdJson).getString("data");
openId = JSONObject.fromObject(openIds).getString("openid");
openId = openId.replace("[", "");
openId = openId.replace("]", "");
openId = openId.replace("\"", "");
String[] splitOpenId = openId.split(",");
return splitOpenId;
}
/**
* 根据openId获取用户信息
* @param openIds
* @return
* @throws Exception
*/
public static List<WeiXinUser> getUserInfo(String[] openIds) throws Exception {
List<WeiXinUser> weiXinUserList = new ArrayList<WeiXinUser>();
for (int i = 0; i < openIds.length; i++) {
String openId = String.valueOf(openIds[i]);
String accessToken = GetAccessToken.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
String userinfo = WeiXinUtlis.sendGet(url);
JSONObject user = JSONObject.fromObject(userinfo);
WeiXinUser weiXinUser = new WeiXinUser();
//用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。
weiXinUser.setState(user.getString("subscribe"));
//用户的标识,对当前公众号唯一
weiXinUser.setOpenid(user.getString("openid"));
//用户的昵称
weiXinUser.setNickname(user.getString("nickname"));
//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
weiXinUser.setSex(user.getString("sex"));
//用户所在城市
weiXinUser.setCity(user.getString("city"));
//国家
weiXinUser.setCountry(user.getString("country"));
//省份
weiXinUser.setProvince(user.getString("province"));
//用户的语言,简体中文为zh_CN
weiXinUser.setLanguage(user.getString("language"));
weiXinUserList.add(weiXinUser);
}
return weiXinUserList;
}
}
public class WeiXinUtlis {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return result 所代表远程资源的响应结果
*/
public static String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
// Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
// for (String key : map.keySet()) {
// System.out.println(key + "--->" + map.get(key));
// }
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
public class WeiXinUser {
// 唯一标识(openid)
private String openid;
// 昵称
private String nickname;
// 性别
private String sex;
// 城市
private String city;
// 国家
private String country;
// 省份
private String province;
// 语言
private String language;
// 头像
private String headimgurl;
// 最后关注时间
private String subscribe_time;
// 关注状态 关注/取消关注
private String state;
// 纬度
private String latitude;
// 经度
private String longitude;
//set---get省略
}