1. 程式人生 > >判斷使用者是否取消關注微信公眾號

判斷使用者是否取消關注微信公眾號

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; }