⑴ 怎麼利用java獲取微信聊天窗口給好友發信息
java做微信,你可以嘗試用itchat4j,是java操作微信的包,可以實現微信的登陸,自動回復等
⑵ 寰淇″備綍鑷鍔ㄥ洖澶嶆秷鎮
闅忕潃縐戞妧鐨勫彂灞曪紝寰淇″凡緇忔垚涓轟簡浜轟滑鏃ュ父鐢熸椿涓涓嶅彲緙哄皯鐨勪竴閮ㄥ垎錛屽畠涓嶄粎鍙浠ョ敤鏉ヨ亰澶╋紝榪樺彲浠ョ敤鏉ュ彂甯冧俊鎮銆佹敮浠樿處鍗曠瓑絳夈
鑰屼笖錛屽井淇′篃鎻愪緵浜嗚嚜鍔ㄥ洖澶嶆秷鎮鐨勫姛鑳斤紝鍙浠ュ府鍔╃敤鎴瘋妭鐪佹椂闂達紝鎻愰珮宸ヤ綔鏁堢巼銆
棣栧厛錛岃佷嬌鐢ㄥ井淇$殑鑷鍔ㄥ洖澶嶆秷鎮鍔熻兘錛岄渶瑕佸湪寰淇″叕浼楀彿鍚庡彴璁劇疆鑷鍔ㄥ洖澶嶆秷鎮銆
鍦ㄨ劇疆鑷鍔ㄥ洖澶嶆秷鎮鏃訛紝鍙浠ヨ劇疆鍏抽敭璇嶏紝褰撶敤鎴峰彂閫佺殑娑堟伅涓鍖呭惈鍏抽敭璇嶆椂錛屽井淇′細鑷鍔ㄥ洖澶嶆秷鎮銆
姝ゅ栵紝榪樺彲浠ヨ劇疆鏅鴻兘鍥炲嶏紝褰撶敤鎴峰彂閫佺殑娑堟伅涓娌℃湁鍏抽敭璇嶆椂錛屽井淇′篃鍙浠ユ牴鎹鐢ㄦ埛鍙戦佺殑娑堟伅鍐呭硅嚜鍔ㄥ洖澶嶆秷鎮銆
鍏舵★紝寰淇¤繕鎻愪緵浜嗘ā鏉挎秷鎮鐨勫姛鑳斤紝鍙浠ヨ╃敤鎴瘋劇疆妯℃澘娑堟伅錛屽綋鐢ㄦ埛鍙戦佹秷鎮鏃訛紝寰淇′細鑷鍔ㄦ妸妯℃澘娑堟伅鍙戦佺粰鐢ㄦ埛銆
榪欐牱錛岀敤鎴峰氨鍙浠ヨ妭鐪佹椂闂達紝涓嶇敤姣忔¢兘鎵嬪姩杈撳叆娑堟伅錛岃屾槸鍙浠ョ洿鎺ュ彂閫佹ā鏉挎秷鎮銆
鏈鍚庯紝寰淇¤繕鎻愪緵浜嗗㈡湇娑堟伅鐨勫姛鑳斤紝鍙浠ヨ╃敤鎴瘋劇疆瀹㈡湇娑堟伅錛屽綋鐢ㄦ埛鍙戦佹秷鎮鏃訛紝寰淇′細鑷鍔ㄦ妸瀹㈡湇娑堟伅鍙戦佺粰鐢ㄦ埛銆
榪欐牱錛岀敤鎴峰氨鍙浠ヨ妭鐪佹椂闂達紝涓嶇敤姣忔¢兘鎵嬪姩杈撳叆娑堟伅錛岃屾槸鍙浠ョ洿鎺ュ彂閫佸㈡湇娑堟伅銆
鎬諱箣錛屽井淇$殑鑷鍔ㄥ洖澶嶆秷鎮鍔熻兘鍙浠ュ府鍔╃敤鎴瘋妭鐪佹椂闂達紝鎻愰珮宸ヤ綔鏁堢巼銆
瀹冨彲浠ヨ╃敤鎴瘋劇疆鍏抽敭璇嶃佹櫤鑳藉洖澶嶃佹ā鏉挎秷鎮鍜屽㈡湇娑堟伅錛屼粠鑰岃嚜鍔ㄥ洖澶嶆秷鎮銆
姝ゅ栵紝寰淇¤繕鎻愪緵浜嗗叾浠栧姛鑳斤紝濡傚彂甯冧俊鎮銆佹敮浠樿處鍗曠瓑錛屽彲浠ユ弧瓚崇敤鎴風殑涓嶅悓闇奼傘
⑶ 如何用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){