官方文档地址
获取token
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
public static String GetToken(){
String a = null;
try {
String u3 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=Appid&secret=Secret";
String u4 = u3.replace("Appid", "这里填你自己的微信公众号的appid").replace("Secret", 这里填你自己的微信公众号的secret);
StringBuffer json1 = new StringBuffer();
URL url = new URL(u4);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStreamReader is = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(is);
String temp;
while ((temp = br.readLine())!=null) {
json1.append(temp).append("\n");
JSONObject jsonObject = JSONObject.fromObject(temp);
String token = jsonObject.get("access_token").toString();
System.out.println("全局+++token:"+token);
a = token;
}
return a;
}catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static Object GetJsonObject(String url,String token,String jsonString){
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject9 = new JSONObject();
String u4 = url.replace("ACCESS_TOKEN", token);
StringBuffer buffer = new StringBuffer();
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
URL dizhi = new URL(u4);
HttpURLConnection conn = (HttpURLConnection) dizhi.openConnection();
conn.setRequestMethod("POST");
// conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
if (null != jsonString) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(jsonString.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
System.out.println(jsonObject);
// 遍历jsonobject 将title url thumb_url
Map<String, Object> mapMax = new HashMap<String, Object>();
JSONArray jsonArray = jsonObject.getJSONArray("item");
List<Object> list = new ArrayList<Object>();
for (int j = 0; j < jsonArray.size(); j++) {
JSONObject jsonObject1 = (JSONObject)jsonArray.get(j);
JSONArray jsonArray2 = jsonObject1.getJSONObject("content").getJSONArray("news_item");
for (int i = 0; i < jsonArray2.size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
JSONObject jsonObject2 = (JSONObject)jsonArray2.get(i);
map.put("title", jsonObject2.get("title"));//标题
map.put("url", jsonObject2.get("url"));//详情页地址
map.put("thumb_url", jsonObject2.get("thumb_url"));//缩略图地址
map.put("create_time",jsonObject1.getJSONObject("content").get("create_time")+"000");
list.add(map);
}
}
JSONArray jsonarr = JSONArray.fromObject(list);
jsonObject9.put("list", jsonarr);
jsonObject9.put("count", jsonArray.size());
jsonObject9.put("code", "200");
jsonObject9.put("msg", "success");
}catch(Exception e){
e.printStackTrace();
}
return jsonObject9;
}
获取素材
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
public static Object startTimer(String offset){
JSONObject getJsonObject = new JSONObject();
if (Tools.notEmpty(offset)) {
String token = GetToken();
// 通过token获取素材
MaterialParam para = new MaterialParam();//调用接口所需要的参数实体类
para.setType("news");
para.setOffset(Integer.parseInt(offset));
para.setCount(20);
JSONObject jsonObject = new JSONObject();
jsonObject = JSONObject.fromObject(para);
String outputStr = jsonObject.toString();//将参数对象转换成json字符串
getJsonObject = (JSONObject)GetJsonObject("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN", token,outputStr);
System.out.println(getJsonObject);
}else {
getJsonObject.put("code", 300);
getJsonObject.put("msg", "参数不能为空");
}
return getJsonObject;
}
- 注
曾拿微信小程序获取调用此素材列表接口,却一直请求失败,一直提示媒体类型无效,不明白是什么原因,你知道就告诉我吧,谢谢!!
- 另附实体类代码
/*获取素材列表调用接口所需要的参数实体类**/
public class MaterialParam {
private String type;//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
private int offset;//从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
private int count;//返回素材的数量,取值在1到20之间
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}