微信公众平台接口的调用

1、微信公众平台测试号的申请地址:

  https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421137522

  

2、微信公众号认证

  参考地址:http://kf.qq.com/product/weixinmp.html#hid=99

3、获取token以及图文统计接口的调用

  1 package com;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.InputStreamReader;
  5 import java.io.OutputStream;
  6 import java.net.HttpURLConnection;
  7 import java.net.URL;
  8 import java.util.HashMap;
  9 import java.util.Iterator;
 10 import java.util.Map;
 11 
 12 import net.sf.json.JSONException;
 13 import net.sf.json.JSONObject;
 14 
 15 public class testTjtw {
 16 
 17     public static String accesstoken=null;
 18     static {
 19         try{
 20             accesstoken=getToken();
 21         }catch(Exception e){
 22             e.printStackTrace();
 23             
 24         }
 25     }
 26     
 27 
 28 
 29 
 30 
 31     
 32     //统计图文消息
 33     public static void getTemp()throws Exception{
 34         
 35         String begin_date = "2018-05-09";
 36         String end_date = "2018-05-09";
 37         
 38         StringBuffer strbuf=new StringBuffer();
 39         strbuf.append("{");
 40         strbuf.append(""begin_date":"").append(begin_date).append("",");
 41         strbuf.append(""end_date":"").append(end_date).append(""" );
 42         strbuf.append("}");
 43         String param=strbuf.toString();
 44         //获取图文群发每日数据(getarticlesummary)
 45         String address="https://api.weixin.qq.com/datacube/getarticlesummary?";    
 46         //获取图文群发总数据(getarticletotal)
 47         //String address="https://api.weixin.qq.com/datacube/getarticletotal?";    
 48         //获取图文统计数据(getuserread)
 49         //String address="https://api.weixin.qq.com/datacube/getuserread?";
 50         //获取图文统计分时数据(getuserreadhour)
 51         //String address="https://api.weixin.qq.com/datacube/getuserreadhour?";    
 52 
 53         String ACCESS_TOKEN=accesstoken;
 54         address=address+"access_token="+ACCESS_TOKEN;
 55 
 56         String Strjson=httpPost(address,param);
 57         
 58         System.out.println("原始数据:");
 59         System.out.println(Strjson.toString()); 
 60     }
 61     
 62 
 63     public static String getToken ()throws Exception{
 64         //wx9a86c4406356f6cf
 65         //String appid="wxfc27805daac56d9b";
 66         String appid="wx9a86c4406356f6cf";
 67         //0e947a22d4317dd638cf0e13f4985cd2
 68         String secret="0e947a22d4317dd638cf0e13f4985cd2";
 69         //String secret="12d853529003c68d0d2c9d4f87dd8b57";
 70         //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxfc27805daac56d9b&secret=12d853529003c68d0d2c9d4f87dd8b57
 71         String address="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";        
 72         String param="&appid="+appid+"&secret="+secret;
 73         address = address + param;
 74         param = null;
 75         String Strjson=httpPost(address,param);
 76         Map<String,String> mapst=toMap(Strjson);
 77 
 78         String token=mapst.get("access_token");
 79         System.out.println(token);
 80         return token;
 81         
 82     }
 83 
 84     //httpPost请求
 85     public static String httpPost (String address,String param)throws Exception{
 86 
 87         URL url = null;
 88         HttpURLConnection urlConn = null;
 89         url = new URL(address);
 90         urlConn = (HttpURLConnection) url.openConnection();
 91         urlConn.setRequestMethod("POST");
 92         urlConn.setDoOutput(true);
 93         OutputStream out = urlConn.getOutputStream();
 94         if(param!=null&&!"".equals(param)){
 95             out.write(param.toString().getBytes("utf-8"));
 96         }
 97         
 98         out.flush();
 99         out.close();
100 
101         StringBuffer str = new StringBuffer(); 
102         BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8")); 
103         String inputLine = null; 
104         while ( (inputLine = in.readLine()) != null){ 
105             str.append(inputLine); 
106         } 
107         in.close(); 
108         return str.toString();
109     }
110     /**
111      * 将Json对象转换成Map
112      * 
113      * @param jsonObject
114      *            json对象
115      * @return Map对象
116      * @throws JSONException
117      */
118     public static Map<String,String> toMap(String jsonString) throws JSONException {
119 
120         JSONObject jsonObject = new JSONObject();
121         jsonObject=JSONObject.fromObject(jsonString);
122         
123         Map<String,String> result = new HashMap<String,String>();
124         Iterator iterator = jsonObject.keys();
125         String key = null;
126         String value = null;
127         
128         while (iterator.hasNext()) {
129             key = (String) iterator.next();
130             value = jsonObject.getString(key);
131             result.put(key, value);
132 
133         }
134         return result;
135 
136     }
137     
138     public static void main(String[] args) throws Exception {
139         testTjtw tj = new testTjtw();
140         tj.getTemp();
141         
142     }
143     
144 }
获取token以及图文统计接口的调用
原文地址:https://www.cnblogs.com/ouyy/p/9007582.html