Detailed description of the development instance of the WeChat public account customer service interface and the instance details

Source: Internet
Author: User
Tags openid

Detailed description of the development instance of the Public Account customer service interface, and detailed description of the Instance

After the platform was updated, we found that the customer service interface was good. I will share my research with you.

According to the official documentation, you can send the specified JSon to the customer service interface.

First, encapsulate the JSon class:

Package com. lwz. wx. bean. kf; // This is the outermost layer, which can also be the base class, public class Basebean {private String touser; private String msgtype; public String getTouser () {return touser ;} public void setTouser (String touser) {this. touser = touser;} public String getMsgtype () {return msgtype;} public void setMsgtype (String msgtype) {this. msgtype = msgtype ;}}
// This class inherits the base class, package com. lwz. wx. bean. kf; public class BaseNews extends Basebean {private Kfnews news; public Kfnews getNews () {return news;} public void setNews (Kfnews news) {this. news = news ;}}//
package com.lwz.wx.bean.kf;import java.util.List;public class Kfnews {private List<articles> articles;public List<articles> getArticles() {return articles;}public void setArticles(List<articles> articles) {this.articles = articles;}}//
package com.lwz.wx.bean.kf;public class articles {private String title;  private String description;  private String url;  private String picurl;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getPicurl() {return picurl;}public void setPicurl(String picurl) {this.picurl = picurl;}}

The above structure corresponds

The next step is to create the JSON data.

Package com. lwz. wx. main; import java. util. arrayList; import java. util. list; import org. apache. log4j. logger; import net. sf. json. JSONObject; import com. lwz. wx. bean. accessToken; import com. lwz. wx. bean. articles; import com. lwz. wx. bean. kf. baseNews; import com. lwz. wx. bean. kf. baseText; import com. lwz. wx. bean. kf. basebean; import com. lwz. wx. bean. kf. kfnews; import com. lwz. wx. bean. kf. articles; import com. lwz. wx. bean. kf. t Ext; import com. lwz. wx. util. weixinUtil; public class KfManager {private final static Logger log = Logger. getLogger (Basebean. class); public static void Gotokf (String openid) {String appId = ""; // enter your own APPID and the String appSecret = "" to be authenticated ""; // call the interface to obtain access_tokenAccessToken at = WeixinUtil. getAccessToken (appId, appSecret); if (null! = At) {// call the interface to send the message int result = WeixinUtil. runkf (getkfnews (openid),. getToken (); // This method will be shown below // int result = WeixinUtil. createMenu (getMenu (), "1832148947"); // determine the menu creation result if (0 = result) log.info ("call customer service information sent successfully! "); Elselog.info (" Call failed, error code: "+ result );}}
Private static BaseNews getkfnews (String openid) {articles art1 = new articles (); art1.setDescription ("1"); art1.setPicurl ("http://www.baidu.com"); art1.setTitle ("Test 1 "); art1.setUrl ("http://www.baidu.com"); articles art2 = new articles (); art2.setDescription ("1"); art2.setPicurl ("http://www.baidu.com"); art2.setTitle ("Test 1 "); art2.setUrl ("http://www.baidu.com"); List <articles> list = new ArrayList <articles> (); Kfn Ews news = new Kfnews (); list. add (art1); list. add (art2); news. setArticles (list); BaseNews kfbean = new BaseNews (); kfbean. setMsgtype ("news"); kfbean. setTouser (openid); kfbean. setNews (news); String jsonkfbean = JSONObject. fromObject (kfbean ). toString (); System. out. println (jsonkfbean); return kfbean;} private static BaseText getkftext (String openid) {text = new text (); text. setContent ("text content"); BaseText textbean = n Ew BaseText (); textbean. setMsgtype ("text"); textbean. setTouser (openid); textbean. setText (text); String jsonkfbean = JSONObject. fromObject (textbean ). toString (); System. out. println (jsonkfbean); return textbean;} // The method for calling an interface is as follows: public static String kf_news_url = "https://api.weixin.qq.com/cgi-bin/message/custom/send? Access_token = ACCESS_TOKEN "; public static int Runkf (Basebean getkfnews, String token) {int result = 0; // construct the created urlString url = kf_news_url.replace (" ACCESS_TOKEN ", token ); // convert the object to a json String jsonnews = JSONObject. fromObject (getkfnews ). toString (); // System. out. println (jsonMenu); // call the interface to create JSONObject jsonObject = httpRequest (url, "POST", jsonnews); if (null! = JsonObject) {if (0! = JsonObject. getInt ("errcode") {result = jsonObject. getInt ("errcode"); log. error ("failed to call customer service interface errcode :{} errmsg :{}") ;}} return result ;}

This is done. There may be more. This is also true for other texts.

I hope to help you learn and refer to this part. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.