码迷,mamicode.com
首页 > 微信 > 详细

测试微信公众平台图文统计接口

时间:2018-05-08 14:36:07      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:json   main   key   pos   except   net   拼接   auto   public   

package com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class testTongji {
//获取token
public static String accesstoken=null;
static {
try{
accesstoken=getToken();
}catch(Exception e){
e.printStackTrace();

}
}

public static String getToken ()throws Exception{
String userName="getTokenUser";
String password="123456";

String address="http://192.169.1.47/:8081/szset/weix_getToken.htm";
String param="userName="+userName+"&password="+password;

String Strjson=httpPost(address,param);
Map<String,String> mapst=toMap(Strjson);

String token=mapst.get("token");
System.out.println(token);
return token;
}

//httpPost请求
public static String httpPost (String address,String param)throws Exception{

URL url = null;
HttpURLConnection urlConn = null;
url = new URL(address);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
OutputStream out = urlConn.getOutputStream();
if(param!=null&&!"".equals(param)){
out.write(param.toString().getBytes("utf-8"));
}

out.flush();
out.close();

StringBuffer str = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
String inputLine = null;
while ( (inputLine = in.readLine()) != null){
str.append(inputLine);
}
in.close();
return str.toString();
}

/**
* 将Json对象转换成Map
*
* @param jsonObject
* json对象
* @return Map对象
* @throws JSONException
*/
public static Map<String,String> toMap(String jsonString) throws JSONException {

JSONObject jsonObject = new JSONObject();
jsonObject=JSONObject.fromObject(jsonString);

Map<String,String> result = new HashMap<String,String>();
Iterator iterator = jsonObject.keys();
String key = null;
String value = null;

while (iterator.hasNext()) {
key = (String) iterator.next();
value = jsonObject.getString(key);
result.put(key, value);

}
return result;

}

//测试统计数据
public static void testTJ() throws Exception{
//https://api.weixin.qq.com/datacube/getarticlesummary?access_token=ACCESS_TOKEN
String address="https://api.weixin.qq.com/datacube/getarticlesummary?";
String ACCESS_TOKEN=accesstoken;
address=address+"access_token="+ACCESS_TOKEN;
/**
* {
"begin_date": "2014-12-08",
"end_date": "2014-12-08"
}
*/

//拼接参数
StringBuffer strbuf=new StringBuffer();
String begin_date = "2014-12-08";
String end_date = "2015-12-08";

strbuf.append("{");
strbuf.append("\"begin_date\":\"").append(begin_date).append("\",");
strbuf.append("\"end_date\":\"").append(end_date).append("\"" );
strbuf.append("}");
String param=strbuf.toString();


String Strjson=httpPost(address,param);

System.out.println("原始数据:");
System.out.println(Strjson.toString());
}

public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer strbuf=new StringBuffer();
String begin_date = "2014-12-08";
String end_date = "2015-12-08";

strbuf.append("{");
strbuf.append("\"begin_date\":\"").append(begin_date).append("\",");
strbuf.append("\"end_date\":\"").append(end_date).append("\"" );
strbuf.append("}");
String param=strbuf.toString();
System.out.println("param:"+param);

}

}

测试微信公众平台图文统计接口

标签:json   main   key   pos   except   net   拼接   auto   public   

原文地址:https://www.cnblogs.com/ouyy/p/9007582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!