/// <summary> /// 从JObject对象解析 /// </summary> /// <param name="jo"></param> public override void Parse(JObject jo) { base.Parse(jo); JToken jt; if (jo.TryGetValue("content", out jt) && jt.Type == JTokenType.Object && ((JObject)jt).TryGetValue("news_item", out jt) && jt.Type == JTokenType.Array && ((JArray)jt).Count > 0) { JArray ja = (JArray)jt; Content = new MultiMediaArticle[ja.Count]; for (int i = 0; i < ja.Count; i++) { Content[i] = new MultiMediaArticle((JObject)ja[i]); } } else { Content = null; } }
/// <summary> /// 获取永久图文素材 /// </summary> /// <param name="userName">公众号</param> /// <param name="mediaId">图文素材的id</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回图文消息;如果获取失败,返回null。</returns> public static MultiMediaArticle[] GetNews(string userName, string mediaId, out ErrorMessage errorMessage) { byte[] responseData = Get(userName, mediaId); if (responseData == null || responseData.Length == 0) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取永久图文素材失败。"); return(null); } string responseContent = HttpHelper.ResponseEncoding.GetString(responseData); if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); return(null); } else { JObject jo = JObject.Parse(responseContent); JToken jt; if (jo.TryGetValue("news_item", out jt)) { errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取永久图文素材成功。"); JArray ja = (JArray)jt; MultiMediaArticle[] articles = new MultiMediaArticle[ja.Count]; for (int i = 0; i < articles.Length; i++) { articles[i] = new MultiMediaArticle((JObject)ja[i]); } return(articles); } else { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取永久图文素材失败。"); return(null); } } }
/// <summary> /// 新增永久图文素材 /// </summary> /// <param name="userName">公众号</param> /// <param name="articles">图文消息</param> /// <param name="errorMessage">返回新增是否成功</param> /// <returns>返回图文消息的媒体id;如果失败,返回null。</returns> public static string Add(string userName, IEnumerable <MultiMediaArticle> articles, out ErrorMessage errorMessage) { string responseContent = HttpHelper.RequestResponseContent(urlForAddingNews, userName, null, httpMethodForEditing, MultiMediaArticle.ToJson(articles)); return(GetMediaId(responseContent, out errorMessage)); }