欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序獲取公眾號(hào)文章列表及顯示文章的示例代碼

 更新時(shí)間:2020年03月10日 16:08:20   作者:YO_RUI  
這篇文章主要介紹了微信小程序獲取公眾號(hào)文章列表及顯示文章的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

微信小程序中如何打開公眾號(hào)中的文章,步驟相對(duì)來(lái)說(shuō)不麻煩。

1、公眾號(hào)設(shè)置

小程序若要獲取公眾號(hào)的素材,公眾號(hào)需要做一些設(shè)置。

1.1 綁定小程序

公眾號(hào)需要綁定目標(biāo)小程序,否則無(wú)法打開公眾號(hào)的文章。
在公眾號(hào)管理界面,點(diǎn)擊小程序管理 --> 關(guān)聯(lián)小程序


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

1.2 公眾號(hào)開發(fā)者功能配置

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


(2) 開啟開發(fā)者秘密(AppSecret),注意保存改秘密。
(3) 設(shè)置ip白名單,這個(gè)就是發(fā)起請(qǐng)求的機(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是公眾號(hào)的全局唯一接口調(diào)用憑據(jù),公眾號(hào)調(diào)用各接口時(shí)都需使用access_token。API文檔

private String getToken() throws MalformedURLException, IOException, ProtocolException {
		// access_token接口https請(qǐng)求方式: 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 = "公眾號(hào)的開發(fā)者ID(AppID)";
		String secret = "公眾號(hào)的開發(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 獲取文章列表

API文檔

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();
	}

測(cè)試:

@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ù)說(shuō)明查看上面的API文檔



其中第二張圖片中的url即為公眾號(hào)文章的地址,獲取到多少片tem項(xiàng)中就會(huì)有多少項(xiàng),只要得到上面的結(jié)果那么在小程序中打開公眾號(hào)文章已經(jīng)成功一大半了。

最后在小程序中利用<web-view src="...."></web-view>組件打開即可,src中為文章的url地址。

到此這篇關(guān)于微信小程序獲取公眾號(hào)文章列表及顯示文章的示例代碼的文章就介紹到這了,更多相關(guān)小程序獲取公眾號(hào)文章列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論