① 微信公眾平台開發需要學習什麼
微信公眾平台開發時,需要學習知識如下:
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、後台要看你自己了。