Java 判断用户关注微信公众号的状态

public  boolean judgeIsFollow(String access_token,String openid){
    Integer subscribe = 0;
    String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
    try {
        URL urlGet = new URL(url);
        HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
        http.setRequestMethod("GET"); // 必须是get方式请求 
        http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        http.setDoOutput(true);
        http.setDoInput(true);
        http.connect();
        InputStream is = http.getInputStream();
        int size = is.available();
        byte[] jsonBytes = new byte[size];
        is.read(jsonBytes);
        String message = new String(jsonBytes, "UTF-8");
        JSONObject demoJson = JSONObject.parseObject(message);
        System.out.println("JSON字符串:"+demoJson); 
        subscribe = demoJson.getIntValue("subscribe"); // 此字段为关注字段  关注为1 未关注为0
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 1==subscribe?true:false;
}

猜你喜欢

转载自blog.csdn.net/J_M_S_H_T/article/details/86137527