微信公众号开发者模式Java笔记-管理素材

/**
     * 获取素材列表
     */
    public JSONObject getMaterialList(String type ,int offset, int count) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + ACCESS_TOKEN;
        String outputStr = "";
        //POST请求发送的json参数
        Material material = new Material();
        material.setType(type);
        material.setOffset(offset);
        material.setCount(count);

        JSONObject jsonObject = new JSONObject();
        jsonObject = JSONObject.fromObject(material);
        System.out.println(jsonObject);

        outputStr = jsonObject.toString();

        new HttpRequestPost();
        jsonObject = HttpRequestPost.httpRequest(requestUrl, outputStr, "POST");
        return jsonObject;
    }

获取image素材返回的json格式
微信公众号开发者模式Java笔记-管理素材_第1张图片
获取news素材返回的json格式
微信公众号开发者模式Java笔记-管理素材_第2张图片
解析json返回值

/**
     * 解析getMaterialList获取的json数据 to list for news
     * 
     * @param jsonObject
     * @return
     */
    public List MyJsonToObject(JSONObject jsonObject) {
        JSONArray jsonArray = jsonObject.getJSONArray("item");
        System.out.println(jsonArray);

        List lists = new ArrayList();
        System.out.println(jsonArray.size());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = (JSONObject) jsonArray.get(i);
            json = json.getJSONObject("content");
            System.out.println(json);

            JSONArray arr = json.getJSONArray("news_item");
            json = (JSONObject) arr.get(0);

            MaterialNews news = new MaterialNews();
            String title = json.getString("title");
            String author = json.getString("author");
            String digest = json.getString("digest");
            String thumb_media_id = json.getString("thumb_media_id");
            System.out.println(thumb_media_id);
            String url = json.getString("url");
            String content = json.getString("content");
            news.setTitle(title);
            news.setAuthor(author);
            news.setDigest(digest);
            news.setThumb_media_id(thumb_media_id);
            news.setUrl(url);
            news.setContent("NOTNEED");
            lists.add(news);
        }
        return lists;
    }

    /**
     * 解析getMaterialList获取的json数据 to list for image
     * 
     * @param jsonObject
     * @return
     */
    public List ImageMyJsonToObject(JSONObject jsonObject) {
        List list = new ArrayList();
        JSONArray jsonArray = jsonObject.getJSONArray("item");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = (JSONObject) jsonArray.get(i);
            MaterialImage image = new MaterialImage();
            image.setMedia_id(json.getString("media_id"));
            image.setName(json.getString("name"));
            try {
                /**旧的image 没有url 需处理异常 新添加的有url*/
                image.setUrl(json.getString("url"));
            } catch (Exception e) {
                System.out.println("url 不存在异常");
            }
            list.add(image);
        }
        return list;
    }

你可能感兴趣的:(微信公众号开发者模式)