Esempio n. 1
0
  // 上传图文消息
  public static JSONObject uploadNews(List<MsgNews> msgNewsList, String appId, String appSecret) {
    JSONObject rstObj = new JSONObject();
    try {
      JSONArray jsonArr = new JSONArray();
      for (MsgNews news : msgNewsList) {
        JSONObject jsonObj = new JSONObject();
        JSONObject mediaObj =
            WxApi.uploadMedia(MediaType.Image.toString(), news.getPicdir(), appId, appSecret);
        String mediaId = mediaObj.getString("media_id");
        jsonObj.put("thumb_media_id", mediaId);
        if (news.getAuthor() != null) {
          jsonObj.put("author", news.getAuthor());
        } else {
          jsonObj.put("author", "");
        }
        if (news.getTitle() != null) {
          jsonObj.put("title", news.getTitle());
        } else {
          jsonObj.put("title", "");
        }
        if (news.getFromurl() != null) {
          jsonObj.put("content_source_url", news.getFromurl());
        } else {
          jsonObj.put("content_source_url", "");
        }
        if (news.getBrief() != null) {
          jsonObj.put("digest", news.getBrief());
        } else {
          jsonObj.put("digest", "");
        }
        if (news.getShowpic() != null) {
          jsonObj.put("show_cover_pic", news.getShowpic());
        } else {
          jsonObj.put("show_cover_pic", "0");
        }
        jsonObj.put("content", news.getDescription());
        jsonArr.add(jsonObj);
      }
      JSONObject postObj = new JSONObject();
      postObj.put("articles", jsonArr);

      String token = WxApi.getTokenUrl(appId, appSecret);
      rstObj =
          WxApi.httpsRequest(WxApi.getUploadNewsUrl(token), HttpMethod.POST, postObj.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return rstObj;
  }
Esempio n. 2
0
 // 获取 MsgResponseNews 对象
 public static MsgResponseNews getMsgResponseNews(MsgRequest msgRequest, List<MsgNews> msgNews) {
   if (msgNews != null && msgNews.size() > 0) {
     MsgResponseNews responseNews = new MsgResponseNews();
     responseNews.setToUserName(msgRequest.getFromUserName());
     responseNews.setFromUserName(msgRequest.getToUserName());
     responseNews.setMsgType(MsgType.News.toString());
     responseNews.setCreateTime(new Date().getTime());
     responseNews.setArticleCount(msgNews.size());
     List<Article> articles = new ArrayList<Article>(msgNews.size());
     for (MsgNews n : msgNews) {
       Article a = new Article();
       a.setTitle(n.getTitle());
       a.setPicUrl(n.getPicpath());
       if (StringUtils.isEmpty(n.getFromurl())) {
         a.setUrl(n.getUrl());
       } else {
         a.setUrl(n.getFromurl());
       }
       a.setDescription(n.getBrief());
       articles.add(a);
     }
     responseNews.setArticles(articles);
     return responseNews;
   } else {
     return null;
   }
 }