知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 公眾號相關(guān) >
微信小程序獲取關(guān)聯(lián)微信公眾號文章列表
發(fā)表時間:2020-11-6
發(fā)布人:葵宇科技
瀏覽次數(shù):184
微信小程序獲取關(guān)聯(lián)微信公眾號文章列表
首先應(yīng)在公眾號里面點擊“小程序管理”,關(guān)聯(lián)上對應(yīng)的小程序,輸入小程序AppID綁定,到此綁定算是完成
接下來就是要得到AppID和AppSecret,這個在小程序管理中即可查看
下一步就是獲取access_token,這個案例用的是Java所寫
代碼如下:
private String getToken() throws MalformedURLException, IOException, ProtocolException {
grant_type=client_credential&appid=APPID&secret=APPSECRET
String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
String appid = "*********"; //公眾號的開發(fā)者ID(AppID)
String secret = "********"; //公眾號的開發(fā)者密碼(AppSecret)
URL url = new URL(path+"&appid=" + appid + "&secret=" + secret);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream in = connection.getInputStream();
byte[] b = new byte[100];
int len = -1;
StringBuffer sb = new StringBuffer();
while((len = in.read(b)) != -1) {
sb.append(new String(b,0,len));
}
in.close();
return sb.toString();
}
獲取到token后,用token為憑證拉取文章列表
/**
*
* @param
* @return
* @throws IOException
*/
@ResponseBody
@RequestMapping(value = "/getContentList",method = RequestMethod.GET)
private String getContentList(String offset) throws IOException {
String result1 = getWxAppToken();
String content = null;
System.out.println("==offset====="+offset); //這邊是相當(dāng)于自己查詢分頁 目前值為0,也可以是1-N,根據(jù)文章篇幅定義,這里場景是分頁效果
Map<String,Object> token1 = (Map<String, Object>) JSON.parseObject(result1);
//String result2 = getContentList(token1.get("access_token").toString());
String path = " https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token1.get("access_token").toString();
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("content-type", "application/json");
connection.connect();
// post發(fā)送的參數(shù)
Map<String, Object> map = new HashMap<>();
map.put("type", "news"); // news表示圖文類型的素材,具體看API文檔
map.put("offset", Integer.valueOf(offset));
map.put("count", 1); //count為有多少個模塊篇幅,這邊是一個是4篇文章
// 將map轉(zhuǎn)換成json字符串
String paramBody = JSON.toJSONString(map);
OutputStream out = connection.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write(paramBody); // 向流中寫入?yún)?shù)字符串
bw.flush();
InputStream in = connection.getInputStream();
byte[] b = new byte[100];
int len = -1;
StringBuffer sb = new StringBuffer();
while((len = in.read(b)) != -1) {
sb.append(new String(b,0,len));
}
in.close();
JSONObject json = JSONObject.parseObject(sb.toString());
//以上是已經(jīng)獲取到文章列表,下面是視業(yè)務(wù)場景進(jìn)行json操作,如不需要則直接返回sb.toString()。
//取出json中的item
String item=json.getString("item");
//item為數(shù)組json類型,這時需要轉(zhuǎn)換成JSONArray
JSONArray jsonArray = JSONObject.parseArray(item);
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
content = jsonObject.getString("content");
}
//content為文章模塊
JSONObject jsonObject = JSON.parseObject(content);
//取出文章列表信息并轉(zhuǎn)成json
String news = jsonObject.getString("news_item");
JSONArray jsonArray1 = JSONObject.parseArray(news);
List<JsonEntity> arrayList = new ArrayList<JsonEntity>();
//循環(huán)數(shù)組json
for (int i = 0; i < jsonArray1.size(); i++){
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
JsonEntity jsonEntity = new JsonEntity();
jsonEntity.setTitle(jsonObject1.getString("title"));
jsonEntity.setThumb_url(jsonObject1.getString("thumb_url"));
jsonEntity.setUrl(jsonObject1.getString("url"));
arrayList.add(jsonEntity);
}
return JSON.toJSONString(arrayList);
}
在此也為補(bǔ)強(qiáng)自己的json操作,哈哈哈哈
package com.example.demo.json;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.demo.common.Person;
public class JsonLib {
//json字符串-簡單對象型
private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
//json字符串-數(shù)組類型
private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
//復(fù)雜格式j(luò)son字符串
private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
@SuppressWarnings("unchecked")
public static void main(String[] args) {
//demoJson();
//testJSONStrToJSONObject();//json字符串轉(zhuǎn)化對象
//testJSONStrToJSONArray();//json數(shù)組轉(zhuǎn)化json對象
testComplexJSONStrToJSONObject();//json對象嵌套json對象
}
/**
* 復(fù)雜json格式字符串與JSONObject之間的轉(zhuǎn)換
*/
public static void testComplexJSONStrToJSONObject(){
System.out.println(COMPLEX_JSON_STR);
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因為JSONObject繼承了JSON,所以這樣也是可以的
System.out.println(jsonObject);
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
System.out.println(teacherName+"------"+teacherAge+"===json對象===="+course+"----json數(shù)組----"+students);
JSONArray jsonArray = JSON.parseArray(students.toString());
System.out.println(jsonArray);
}
/**
* json字符串-數(shù)組類型與JSONArray之間的轉(zhuǎn)換
*/
public static void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因為JSONArray繼承了JSON,所以這樣也是可以的
//遍歷方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
//遍歷方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
/**
* json字符串-簡單對象型與JSONObject之間的轉(zhuǎn)換
*/
public static void testJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因為JSONObject繼承了JSON,所以這樣也是可以的
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
public static void demoJson() {
/**
* 將 Json 形式的字符串轉(zhuǎn)換為 Map
*/
String str = "{\"name\":\"Tom\",\"age\":90}";
JSONObject jsonObject = JSONObject.parseObject(str);
Map<String, String> params = JSONObject.parseObject(jsonObject.toString(), new TypeReference<Map<String, String>>(){});
System.out.println(params);
/**
* 將 Json 形式的字符串轉(zhuǎn)換為 JavaBean
*/
str = "{\"id\":\"A001\",\"name\":\"Jack\"}";
jsonObject = JSONObject.parseObject(str);
System.out.println(jsonObject);
Person person = JSON.parseObject(str, new TypeReference<Person>() {});
System.out.println(person.toString());
}
}
如有更好的方式及有可以優(yōu)化的地方,可以留言討論學(xué)習(xí)鴨
相關(guān)案例查看更多
相關(guān)閱讀
- vue開發(fā)小程序
- 報廢車拆解管理系統(tǒng)
- 報廢車回收管理系統(tǒng)
- 云南網(wǎng)站建設(shè)快速優(yōu)化
- 網(wǎng)站搭建
- 云南軟件定制公司
- 云南網(wǎng)站建設(shè)專業(yè)品牌
- uniapp開發(fā)小程序
- 云南小程序設(shè)計
- 網(wǎng)站建設(shè)百度官方
- 小程序定制開發(fā)
- 百度小程序
- typescript
- 定制小程序開發(fā)
- 買小程序被騙
- 政府網(wǎng)站建設(shè)服務(wù)
- 微信分銷系統(tǒng)
- web開發(fā)
- 云南衛(wèi)視小程序
- 網(wǎng)站建設(shè)優(yōu)化
- 汽車拆解管理軟件
- 汽車拆解系統(tǒng)
- 網(wǎng)站建設(shè)專家
- 分銷系統(tǒng)
- 云南小程序開發(fā)哪家好
- 云南小程序開發(fā)制作
- 區(qū)塊鏈
- 軟件定制公司
- 云南網(wǎng)站建設(shè)
- 企業(yè)網(wǎng)站