① 微信公众平台开发需要学习什么
微信公众平台开发时,需要学习知识如下:
2、开启公众号开发模式,需要了解微信公众平台的原理。
3、微信号是在联网的环境下才能够实现各种功能的。必备条件准备,第一个是外网服务器,让我们的项目部署在上面,第二个是微信公众平台账号.可以多了解微信企业号回调页面开发-企业号信息的回发。
微信公众平台开发步骤:
首先应该是微信服务器与我们的服务器项目的URL地址建立连接,在本地的eclipse中建立了一个web项目,名称为Weixin,其中建立了一个WeixinServlet,那么Servlet就是处理我们服务器与微信服务器通讯的地址,到时war包部署在mopaas云服务器上面,通过浏览器能够访问到:http://外网服务器地址/Weixin/WeixinServlet,那么我们部署的项目就没问题了。开通微信账号后,进入开发者模式,就会让我们输入一个URL地址,就是上面的我们项目工程与微信建立通讯的地址,包括处理的Servlet,http://外网服务器地址/Weixin/WeixinServlet,输入相关参数,进行确定,微信服务器会调用我们定义Servlet的doGet方法,后面进行消息处理是调用post方法,携带相应的参数通过我们的服务器进行校验无误后,将成功的echostr字符串信息返回给我们的微信服务器,那么我们自己申请的微信平台和我们的Servlet就建立连接了,也就意味着这个地址已经成功绑定了,以后我们通过微信发送的信息,将会由微信服务器通过URL地址转到我们的服务器上的Servlet进行处理。
当用户给微信公众号发送消息,文本图片消息或者点击自定义菜单事件的时候,通过我们绑定的URL地址,给公众号发送消息到微信服务器,微信服务器将我们的消息封装成为xml格式的数据,然后将信息提交到我们的服务器上定义处理类的一个post方法中,我们服务器需要做的就是解析微信服务器发送过来的XML格式的字符串,然后进行相应的逻辑处理后,转换为微信输出格式的xml字符串信息,然后通过HttpServletResponse返回给微信服务器,微信服务器再发送到我们的客户端做出响应。
微信中xml接收文本信息的格式,用户发送到微信服务器,微信服务器转换后发送给我们服务器的。微信的消息交互的实现原理图。
② 如何用java开发微信
说明:
本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。
1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:
[java]view plain
packagedemo.servlet;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importdemo.process.WechatProcess;
/**
*微信服务端收发消息接口
*
*@authorpamchen-1
*
*/
{
/**
*ThedoGetmethodoftheservlet.<br>
*
*.
*
*@paramrequest
*
*@paramresponse
*
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
/**读取接收到的xml消息*/
StringBuffersb=newStringBuffer();
InputStreamis=request.getInputStream();
InputStreamReaderisr=newInputStreamReader(is,"UTF-8");
BufferedReaderbr=newBufferedReader(isr);
Strings="";
while((s=br.readLine())!=null){
sb.append(s);
}
Stringxml=sb.toString();//次即为接收到微信端发送过来的xml数据
Stringresult="";
/**判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回*/
Stringechostr=request.getParameter("echostr");
if(echostr!=null&&echostr.length()>1){
result=echostr;
}else{
//正常的微信处理流程
result=newWechatProcess().processWechatMag(xml);
}
try{
OutputStreamos=response.getOutputStream();
os.write(result.getBytes("UTF-8"));
os.flush();
os.close();
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*ThedoPostmethodoftheservlet.<br>
*
*
*post.
*
*@paramrequest
*
*@paramresponse
*
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
}
1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;//服务器地址/项目名/wechat.do
[html]view plain
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description></description>
<display-name></display-name>
<servlet-name>WechatServlet</servlet-name>
<servlet-class>demo.servlet.WechatServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WechatServlet</servlet-name>
<url-pattern>/wechat.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。
下面就讲解其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。
2.1 首先看一下整体流程处理代码,包括:xml数据处理、调用图灵api、封装返回的xml数据。
[java]view plain
packagedemo.process;
importjava.util.Date;
importdemo.entity.ReceiveXmlEntity;
/**
*微信xml消息处理流程逻辑类
*@authorpamchen-1
*
*/
publicclassWechatProcess{
/**
*解析处理xml、获取智能回复结果(通过图灵机器人api接口)
*@paramxml接收到的微信数据
*@return最终的解析结果(xml格式数据)
*/
publicStringprocessWechatMag(Stringxml){
/**解析xml数据*/
ReceiveXmlEntityxmlEntity=newReceiveXmlProcess().getMsgEntity(xml);
/**以文本消息为例,调用图灵机器人api接口,获取回复内容*/
Stringresult="";
if("text".endsWith(xmlEntity.getMsgType())){
result=newTulingApiProcess().getTulingResult(xmlEntity.getContent());
}
/**此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容
*因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息
**/
result=newFormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(),xmlEntity.getToUserName(),result);
returnresult;
}
}
2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:
[java]view plain
packagedemo.entity;
/**
*接收到的微信xml实体类
*@authorpamchen-1
*
*/
publicclassReceiveXmlEntity{
privateStringToUserName="";
privateStringFromUserName="";
privateStringCreateTime="";
privateStringMsgType="";
privateStringMsgId="";
privateStringEvent="";
privateStringEventKey="";
privateStringTicket="";
privateStringLatitude="";
privateStringLongitude="";
privateStringPrecision="";
privateStringPicUrl="";
privateStringMediaId="";
privateStringTitle="";
privateStringDescription="";
privateStringUrl="";
privateStringLocation_X="";
privateStringLocation_Y="";
privateStringScale="";
privateStringLabel="";
privateStringContent="";
privateStringFormat="";
privateStringRecognition="";
publicStringgetRecognition(){
returnRecognition;
}
publicvoidsetRecognition(Stringrecognition){
Recognition=recognition;
}
publicStringgetFormat(){
returnFormat;
}
publicvoidsetFormat(Stringformat){
Format=format;
}
publicStringgetContent(){
returnContent;
}
publicvoidsetContent(Stringcontent){
Content=content;
}
publicStringgetLocation_X(){
returnLocation_X;
}
publicvoidsetLocation_X(StringlocationX){
Location_X=locationX;
}
publicStringgetLocation_Y(){
returnLocation_Y;
}
publicvoidsetLocation_Y(StringlocationY){
Location_Y=locationY;
}
publicStringgetScale(){
returnScale;
}
publicvoidsetScale(Stringscale){
Scale=scale;
}
publicStringgetLabel(){
returnLabel;
}
publicvoidsetLabel(Stringlabel){
Label=label;
}
publicStringgetTitle(){
returnTitle;
}
publicvoidsetTitle(Stringtitle){
Title=title;
}
publicStringgetDescription(){
returnDescription;
}
publicvoidsetDescription(Stringdescription){
Description=description;
}
publicStringgetUrl(){
returnUrl;
}
publicvoidsetUrl(Stringurl){
Url=url;
}
publicStringgetPicUrl(){
returnPicUrl;
}
publicvoidsetPicUrl(StringpicUrl){
PicUrl=picUrl;
}
publicStringgetMediaId(){
returnMediaId;
}
publicvoidsetMediaId(StringmediaId){
MediaId=mediaId;
}
publicStringgetEventKey(){
returnEventKey;
}
publicvoidsetEventKey(StringeventKey){
EventKey=eventKey;
}
publicStringgetTicket(){
returnTicket;
}
publicvoidsetTicket(Stringticket){
Ticket=ticket;
}
publicStringgetLatitude(){
returnLatitude;
}
publicvoidsetLatitude(Stringlatitude){
Latitude=latitude;
}
publicStringgetLongitude(){
returnLongitude;
}
publicvoidsetLongitude(Stringlongitude){
Longitude=longitude;
}
publicStringgetPrecision(){
returnPrecision;
}
publicvoidsetPrecision(Stringprecision){
Precision=precision;
}
publicStringgetEvent(){
returnEvent;
}
publicvoidsetEvent(Stringevent){
Event=event;
}
publicStringgetMsgId(){
returnMsgId;
}
publicvoidsetMsgId(StringmsgId){
MsgId=msgId;
}
publicStringgetToUserName(){
returnToUserName;
}
publicvoidsetToUserName(StringtoUserName){
③ 《微信公众平台应用开发方法、技巧与案例》epub下载在线阅读,求百度网盘云资源
《微信公众平台应用开发》(柳峰)电子书网盘下载免费在线阅读
链接:https://pan..com/s/1IqH140HJQ10-TTXZWwRvPQ
书名:微信公众平台应用开发
作者:柳峰
豆瓣评分:5.9
出版社:机械工业出版社
出版年份:2014-3
页数:319
内容简介:
本书是目前微信公众平台应用开发领域内容最全面、系统和深入的一本书,也是技术版本最新的。由着名的资深微信公众平台应用开发工程师根据最新的微信5.1版撰写,全面解读了微信公众平台开放的所有API的各项功能和用法,系统讲解了微信公众平台应用开发的流程、方法和技巧。更为重要的是,它还深入讲解了微信公众平台应用开发的高级技术和技巧,如何与LBS等多种技术结合使用,如何调用其他第三方的数据和资源,等等。实战性非常强,包含大量小案例和3个有代表性的综合案例。
全书共11章,分为四个部分:第一部分(第1~2章)介绍了公众平台的使用、公众账号的认证、编辑模式的使用等基础知识;第二部分(第3~4章)首先讲解了如何启用开发模式,然后详细讲解了公众平台的消息接口(包括请求校验、请求消息、事件推送和响应消息),包含一个能够接收与响应任何类型消息的项目,读者可以将该项目导出成WAR包,作为公众平台的基础开发包(适用于订阅号和服务号),在开发公众账号时,只需要关注业务逻辑;第三部分(第5~6章)重点介绍了公众平台的自定义菜单接口和高级接口,并配有完整的接口调用示例和说明,读者可以将这部分的接口调用代码作为公众平台的高级开发包;第四部分(第7~11章)首先总结了一些实用的公众平台开发技巧,如使用表情、识别微信浏览器、图文消息使用、公众账号无响应处理、服务多个账号等,然后逐步详细地讲解了“周边搜索”、“猜数字”(游戏)和“聊天机器人”3个综合案例的开发过程,其中还包含如何与其他技术的结合使用及如何调用第三方的数据和资源,学习完本部分,读者完全有能力胜任大型企业公众账号的开发。附录为公众平台接口的返回码说明,以及公众平台接口的调用次数限制说明。
作者简介:
刘运强,网名“柳峰”,资深微信公众平台应用开发工程师,国内微信公众平台应用开发的先驱之一,项目经验丰富。他还是一位资深的Java软件开发工程师和Android/iOS移动应用开发工程师,活跃于CocoaChina、OSChina、CSDN等社区,并在CSDN博客撰写了系列微信公众平台二次开发的教程,深受欢迎并被广泛传播,也因此获得CSDN博客移动开发版块的“博客冠军”。
④ 如何使用java开发微信公众平台接口
1、首先,要在微信公众平台给你的账号申请到“高级功能” ;前台也就是菜单要想个性化设置必须要有这个功能,不然你只能添加菜单和关闭,但不能删除,还有自动回复也是。
2、后台要看你自己了。