微信小程序獲取公眾號文章列表及顯示文章的示例代碼
微信小程序中如何打開公眾號中的文章,步驟相對來說不麻煩。
1、公眾號設(shè)置
小程序若要獲取公眾號的素材,公眾號需要做一些設(shè)置。
1.1 綁定小程序
公眾號需要綁定目標(biāo)小程序,否則無法打開公眾號的文章。
在公眾號管理界面,點(diǎn)擊小程序管理 --> 關(guān)聯(lián)小程序

輸入小程序的AppID搜索,綁定即可。

1.2 公眾號開發(fā)者功能配置
(1) 在公眾號管理界面,點(diǎn)擊開發(fā)模塊中的基本配置選項(xiàng)。

(2) 開啟開發(fā)者秘密(AppSecret),注意保存改秘密。
(3) 設(shè)置ip白名單,這個就是發(fā)起請求的機(jī)器的外網(wǎng)ip,假如是在自己電腦那就是自己電腦的外網(wǎng)ip,若部署到服務(wù)器那就是服務(wù)器的外網(wǎng)ip。

2、獲取文章信息的步驟
以下只是作為演示。
實(shí)際項(xiàng)目中在自己的服務(wù)端程序中獲取,不要在小程序中直接獲取,畢竟要使用到appid、appsecret這些保密性高的參數(shù)。
2.1 獲取access_token
access_token是公眾號的全局唯一接口調(diào)用憑據(jù),公眾號調(diào)用各接口時都需使用access_token。API文檔
private String getToken() throws MalformedURLException, IOException, ProtocolException {
// access_token接口https請求方式: GET https://api.weixin.qq.com/cgi-bin/token?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));
}
System.out.println(sb.toString());
in.close();
return sb.toString();
}
2.2 獲取文章列表
private String getContentList(String token) throws IOException {
String path = " https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token;
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("content-type", "application/json;charset=utf-8");
connection.connect();
// post發(fā)送的參數(shù)
Map<String, Object> map = new HashMap<>();
map.put("type", "news"); // news表示圖文類型的素材,具體看API文檔
map.put("offset", 0);
map.put("count", 1);
// 將map轉(zhuǎn)換成json字符串
String paramBody = JSON.toJSONString(map); // 這里用了Alibaba的fastjson
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();
return sb.toString();
}
測試:
@Test
public void test() throws IOException {
String result1 = getToken();
Map<String,Object> token = (Map<String, Object>) JSON.parseObject(result1);
String result2 = getContentList(token.get("access_token").toString());
System.out.println(result2);
}

轉(zhuǎn)換成json格式,參數(shù)說明查看上面的API文檔


其中第二張圖片中的url即為公眾號文章的地址,獲取到多少片tem項(xiàng)中就會有多少項(xiàng),只要得到上面的結(jié)果那么在小程序中打開公眾號文章已經(jīng)成功一大半了。
最后在小程序中利用<web-view src="...."></web-view>組件打開即可,src中為文章的url地址。

到此這篇關(guān)于微信小程序獲取公眾號文章列表及顯示文章的示例代碼的文章就介紹到這了,更多相關(guān)小程序獲取公眾號文章列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 微信小程序 下拉列表的實(shí)現(xiàn)實(shí)例代碼
- 微信小程序 列表的上拉加載和下拉刷新的實(shí)現(xiàn)
- 微信小程序?qū)崿F(xiàn)給循環(huán)列表添加點(diǎn)擊樣式實(shí)例
- 微信小程序開發(fā)之好友列表字母列表跳轉(zhuǎn)對應(yīng)位置
- 微信小程序 教程之列表渲染
- 微信小程序列表中item左滑刪除功能
- 微信小程序如何調(diào)用新聞接口實(shí)現(xiàn)列表循環(huán)
- 微信小程序wxml列表渲染原理解析
- 微信小程序開發(fā)(一):服務(wù)器獲取數(shù)據(jù)列表渲染操作示例
- 微信小程序文章列表功能完整實(shí)例
相關(guān)文章
showModalDialog 和 showModelessDialog
showModalDialog 和 showModelessDialog...2007-01-01
通過遮罩層實(shí)現(xiàn)浮層DIV登錄的js代碼
遮罩層實(shí)現(xiàn)浮層DIV登錄的效果,想必很多的朋友都有遇到過吧,實(shí)現(xiàn)起來也是很簡單的,下面有個不錯的實(shí)現(xiàn),大家可以感受下2014-02-02
JavaScript中的Reflect對象詳解(ES6新特性)
這篇文章主要介紹了JavaScript中的Reflect對象(ES6新特性)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
uni.getLocation和wx.getLocation方法調(diào)用無效也不返回失敗的解決方案
BootStrap中Datetimepicker和uploadify插件應(yīng)用實(shí)例小結(jié)

