zoukankan      html  css  js  c++  java
  • java调用微信公众平台接口(二)

    接着上一个随笔,分享了上传素材,这一个分享获取永久素材

    1、获取单个图文素材

    如果需要将示例放入集合,需要实体类

    /*获取素材列表参数实体类**/
    public class Material {
        
        private String title;//图文消息的标题
        private String thumb_media_id;//图文消息的封面图片素材id(必须是永久mediaID)
        private String author;//作者
        private String digest;//图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
        private String content;//图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
        private String url;//图文页的URL,或者,当获取的列表是图片素材列表时,该字段是图片的URL
        private Integer show_cover_pic;//是否显示封面,0为false,即不显示,1为true,即显示
        private String content_source_url;//图文消息的原文地址,即点击“阅读原文”后的URL
        private Integer need_open_comment;//Uint32 是否打开评论,0不打开,1打开
        private Integer only_fans_can_comment;//Uint32 是否打开评论,0不打开,1打开L
        
        public Integer getNeed_open_comment() {
            return need_open_comment;
        }
        public void setNeed_open_comment(Integer need_open_comment) {
            this.need_open_comment = need_open_comment;
        }
        public Integer getOnly_fans_can_comment() {
            return only_fans_can_comment;
        }
        public void setOnly_fans_can_comment(Integer only_fans_can_comment) {
            this.only_fans_can_comment = only_fans_can_comment;
        }
        public String getContent_source_url() {
            return content_source_url;
        }
        public void setContent_source_url(String content_source_url) {
            this.content_source_url = content_source_url;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getThumb_media_id() {
            return thumb_media_id;
        }
        public void setThumb_media_id(String thumb_media_id) {
            this.thumb_media_id = thumb_media_id;
        }
        public String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public String getDigest() {
            return digest;
        }
        public void setDigest(String digest) {
            this.digest = digest;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public int getShow_cover_pic() {
            return show_cover_pic;
        }
        public void setShow_cover_pic(int show_cover_pic) {
            this.show_cover_pic = show_cover_pic;
        }
        
    }

     调用方法

    public final static String MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
    
     /**
         * 获取素材单个
         * @param accessToken 获取接口凭证的唯一标识
         * @param type 素材的类型,图片(image)、视频(video)、语音 (voice)
         * @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
         * @param count 返回素材的数量,取值在1到20之间
         * @return
         */
        public static List<Material> getMaterial(String accessToken,String meidaId) { 
            List<Material> lists = new ArrayList<Material>();//定义图文素材实体类集合
            String outputStr="";//定义一个空的参数字符串
            String requestUrl = MATERIAL.replace("ACCESS_TOKEN", accessToken);//替换调access_token
            MesByOpenIdParam para = new MesByOpenIdParam();//调用接口所需要的参数实体类
            Mpnews mpnews = para.new Mpnews();
            //mpnews.setMedia_id("kjqT60HOZg-aS6TuPriow1o1JRYgMZJlte66ytQS14U");
            mpnews.setMedia_id(meidaId);
            JSONObject jsonObject = new JSONObject();
            jsonObject = JSONObject.fromObject(mpnews);
            outputStr = jsonObject.toString();//将参数对象转换成json字符串
            
            jsonObject = httpsRequest(requestUrl, "POST", outputStr);  //发送https请求(请求的路径,方式,所携带的参数)
            // 如果请求成功  
            if (null != jsonObject) {
                try {  
                    JSONArray jsonArray = jsonObject.getJSONArray("news_item");
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject json = (JSONObject) jsonArray.get(i);
                        System.out.println(json);
                        Material entity = new Material();
                        String title = json.getString("title");
                        String thumb_media_id = json.getString("thumb_media_id");
                        String str = json.getString("show_cover_pic");
                        Integer show_cover_pic = Integer.valueOf(str);
                        String author = json.getString("author");
                        String digest = json.getString("digest");
                        String content = json.getString("content");
                        String content_source_url = json.getString("content_source_url");
                        
                        entity.setTitle(title);
                        entity.setThumb_media_id(thumb_media_id);
                        entity.setShow_cover_pic(show_cover_pic);
                        entity.setAuthor(author);
                        entity.setDigest(digest);
                        entity.setContent(content);
                        entity.setContent_source_url(content_source_url);
                        try {
                            /**旧的image 没有url 需处理异常 新添加的有url*/
                            String url = json.getString("url");
                            entity.setUrl(url);
                        } catch (Exception e) {
                            System.out.println("url 不存在异常");
                        }
                        lists.add(entity);
                    }
                } catch (JSONException e) {  
                    accessToken = null;  
                    // 获取Material失败  
                    System.out.println("jsonObject.getInt(errcode)"+jsonObject.getInt("errcode"));
                    System.out.println("jsonObject.getString(errmsg)"+jsonObject.getString("errmsg"));
                    log.error("获取Material失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));  
                }  
            }  
            return lists;  
        }

    这里面需要注意的是  参数对象和json字符串 的互转。

    2、获取图文素材列表

    直接上代码

    
    
    public final static String MATERIAL_LIST = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";

    /**
    * 获取图文(news)素材列表并存入集合中 * @param accessToken 获取接口凭证的唯一标识 * @param type 素材的类型 图文(news) * @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 * @param count 返回素材的数量,取值在1到20之间 * @return */

    public static List<Material> getMaterialNewsList(String accessToken,String type,int offset,int count) { long totalCount = 0;
    String TOTAL_COUNT
    = null ; List<Material> lists = new ArrayList<Material>();//定义图文素材实体类集合 String outputStr="";//定义一个空的参数字符串 String requestUrl = MATERIAL_LIST.replace("ACCESS_TOKEN", accessToken);//替换调access_token MaterialParam para = new MaterialParam();//调用接口所需要的参数实体类 para.setType(type); para.setOffset(offset); para.setCount(count); JSONObject jsonObject = new JSONObject(); jsonObject = JSONObject.fromObject(para); outputStr = jsonObject.toString();//将参数对象转换成json字符串 jsonObject = httpsRequest(requestUrl, "POST", outputStr); //发送https请求(请求的路径,方式,所携带的参数) // 如果请求成功 if (null != jsonObject) { try { TOTAL_COUNT = jsonObject.getString("total_count"); JSONArray jsonArray = jsonObject.getJSONArray("item"); for (int i = 0; i < jsonArray.size(); i++) { JSONObject json = (JSONObject) jsonArray.get(i); String MEDIA_ID = json.getString("media_id"); System.out.println("MEDIA_ID "+MEDIA_ID); json = json.getJSONObject("content"); System.out.println(json); JSONArray arr = json.getJSONArray("news_item"); json = (JSONObject) arr.get(0); Material entity = new Material(); 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"); entity.setTitle(title); entity.setAuthor(author); entity.setDigest(digest); entity.setThumb_media_id(thumb_media_id); entity.setUrl(url); entity.setContent(content); entity.setShow_cover_pic(1); lists.add(entity); } } catch (JSONException e) { accessToken = null; // 获取Material失败 log.error("获取Material失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); } } if(!StringUtils.isEmpty(TOTAL_COUNT)){ totalCount = Long.valueOf(TOTAL_COUNT); } return lists; }

    3、获取其他类型素材

    public final static String MATERIAL_LIST = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";
    
    /**
         * 获取图片(image)、视频(video)、语音 (voice)素材列表并存入集合中
         * @param accessToken 获取接口凭证的唯一标识
         * @param type 素材的类型,图片(image)、视频(video)、语音 (voice)
         * @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
         * @param count 返回素材的数量,取值在1到20之间
         * @return
         */
        public static List<MaterialImage> getMaterialImageList(String accessToken,String type,int offset,int count) {
            long totalCount = 0;
            String TOTAL_COUNT = null ;
            List<MaterialImage> lists = new ArrayList<MaterialImage>();//定义图文素材实体类集合
            String outputStr="";//定义一个空的参数字符串
            String requestUrl = MATERIAL_LIST.replace("ACCESS_TOKEN", accessToken);//替换调access_token
            MaterialParam para = new MaterialParam();//调用接口所需要的参数实体类
            para.setType(type);
            para.setOffset(offset);
            para.setCount(count);
            JSONObject jsonObject = new JSONObject();
            jsonObject = JSONObject.fromObject(para);
            outputStr = jsonObject.toString();//将参数对象转换成json字符串
    
            jsonObject = httpsRequest(requestUrl, "POST", outputStr);  //发送https请求(请求的路径,方式,所携带的参数)
            // 如果请求成功  
            if (null != jsonObject) {
                try {  
                    TOTAL_COUNT = jsonObject.getString("total_count");
                    JSONArray jsonArray = jsonObject.getJSONArray("item");
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject json = (JSONObject) jsonArray.get(i);
                        System.out.println(json);
                        MaterialImage image = new MaterialImage();
                        String mediaId = json.getString("media_id");
                        String name = json.getString("name");
                        String updateTime = json.getString("update_time");
                        image.setUpdateTime(updateTime);
                        image.setMedia_id(mediaId);
                        image.setName(name);
                        try {
                            /**旧的image 没有url 需处理异常 新添加的有url*/
                            String url = json.getString("url");
                            image.setUrl(url);
                        } catch (Exception e) {
                            System.out.println("url 不存在异常");
                        }
                        lists.add(image);
                    }
                } catch (JSONException e) {  
                    accessToken = null;  
                    // 获取Material失败  
                    System.out.println("jsonObject.getInt(errcode)"+jsonObject.getInt("errcode"));
                    System.out.println("jsonObject.getString(errmsg)"+jsonObject.getString("errmsg"));
                    log.error("获取Material失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));  
                }  
            }  
           if(!StringUtils.isEmpty(TOTAL_COUNT)){
                totalCount = Long.valueOf(TOTAL_COUNT);
            }
            
            return lists;  
        }

    其中的实体类

    /*获取素材列表调用接口所需要的参数实体类**/
    public class MaterialParam {
        
        private String type;//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
        
        private int offset;//从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
        
        private int count;//返回素材的数量,取值在1到20之间
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public int getOffset() {
            return offset;
        }
    
        public void setOffset(int offset) {
            this.offset = offset;
        }
    
        public int getCount() {
            return count;
        }
    
        public void setCount(int count) {
            this.count = count;
        }
        
    }
    /**素材图片实体类*/
    public class MaterialImage {
        
        //要获取的素材的media_id
        private String media_id;
        //文件名称
        private String name;
        //图片的URL
        private String url;
        
        private String updateTime;
    
        public String getUpdateTime() {
            return updateTime;
        }
    
        public void setUpdateTime(String updateTime) {
            this.updateTime = updateTime;
        }
    
        public String getMedia_id() {
            return media_id;
        }
    
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
        
        
    }

    我也是在学习中,写的有点乱。

    其实通过这两节的学习,在开发文档中的接口基本能调用。

  • 相关阅读:
    http协议学习系列
    git常用命令大全
    git常用命令与常见面试题总结
    MyBatis框架及原理分析
    Mybatis常见面试题总结
    java实现克隆的三种(很最全面)
    java中equals和==之间的区别?clone方法的作用,及其为什么要使用clone方法?如何使用clone复制对象?以及深克隆浅克隆
    ThreadLocal的简单使用及实现的原理
    Java 最常见的 208 道面试题
    TCP流量控制
  • 原文地址:https://www.cnblogs.com/zmjc/p/11493129.html
Copyright © 2011-2022 走看看