官方文檔地址
獲取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();
}
// 從輸入流讀取返回內(nèi)容
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"));//標(biāo)題
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();//調(diào)用接口所需要的參數(shù)實(shí)體類
para.setType("news");
para.setOffset(Integer.parseInt(offset));
para.setCount(20);
JSONObject jsonObject = new JSONObject();
jsonObject = JSONObject.fromObject(para);
String outputStr = jsonObject.toString();//將參數(shù)對象轉(zhuǎn)換成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", "參數(shù)不能為空");
}
return getJsonObject;
}
- 注
曾拿微信小程序獲取調(diào)用此素材列表接口,卻一直請求失敗,一直提示媒體類型無效,不明白是什么原因,你知道就告訴我吧,謝謝!!
- 另附實(shí)體類代碼
/*獲取素材列表調(diào)用接口所需要的參數(shù)實(shí)體類**/
public class MaterialParam {
private String type;//素材的類型,圖片(image)、視頻(video)、語音 (voice)、圖文(news)
private int offset;//從全部素材的該偏移位置開始返回,0表示從第一個素材 返回
private int count;//返回素材的數(shù)量,取值在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;
}
}
- 返回值實(shí)體類根據(jù)參數(shù)實(shí)體類自行腦補(bǔ),不再贅述
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。