欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

微信小程序獲取關(guān)聯(lián)微信公眾號(hào)文章列表 - 新聞資訊 - 云南小程序開(kāi)發(fā)|云南軟件開(kāi)發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開(kāi)發(fā)/軟件開(kāi)發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >

微信小程序獲取關(guān)聯(lián)微信公眾號(hào)文章列表

發(fā)表時(shí)間:2020-11-6

發(fā)布人:葵宇科技

瀏覽次數(shù):184

微信小程序獲取關(guān)聯(lián)微信公眾號(hào)文章列表

首先應(yīng)在公眾號(hào)里面點(diǎn)擊“小程序管理”,關(guān)聯(lián)上對(duì)應(yīng)的小程序,輸入小程序AppID綁定,到此綁定算是完成

接下來(lái)就是要得到AppID和AppSecret,這個(gè)在小程序管理中即可查看

下一步就是獲取access_token這個(gè)案例用的是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 = "*********"; //公眾號(hào)的開(kāi)發(fā)者ID(AppID)
        String secret = "********"; //公眾號(hào)的開(kāi)發(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)于自己查詢分頁(yè) 目前值為0,也可以是1-N,根據(jù)文章篇幅定義,這里場(chǎng)景是分頁(yè)效果
        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為有多少個(gè)模塊篇幅,這邊是一個(gè)是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ù)場(chǎng)景進(jìn)行json操作,如不需要?jiǎng)t直接返回sb.toString()。
        //取出json中的item
        String item=json.getString("item");
        //item為數(shù)組json類型,這時(shí)需要轉(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字符串-簡(jiǎn)單對(duì)象型
	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)化對(duì)象
		//testJSONStrToJSONArray();//json數(shù)組轉(zhuǎn)化json對(duì)象
		testComplexJSONStrToJSONObject();//json對(duì)象嵌套json對(duì)象
   }
	
	/**
     * 復(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);//因?yàn)镴SONObject繼承了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對(duì)象===="+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);//因?yàn)镴SONArray繼承了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字符串-簡(jiǎn)單對(duì)象型與JSONObject之間的轉(zhuǎn)換
     */
    public static void testJSONStrToJSONObject(){
 
        JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
        //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因?yàn)镴SONObject繼承了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)案例查看更多