本文目錄一覽:
- 1、如何用java開發微信
- 2、Java向微信添加商品名稱
- 3、用Java怎麼實現微信支付
- 4、用Java怎麼實現微信支付?
- 5、如何實現java程序與微信公眾平台之間實現消息推送
- 6、微信支付Java如何判斷回調
如何用java開發微信
說明:
本次的教程主要是對微信公眾平台開發者模式的講解,網路上很多類似文章,但很多都讓初學微信開發的人一頭霧水,所以總結自己的微信開發經驗,將微信開發的整個過程系統的列出,並對主要代碼進行講解分析,讓初學者儘快上手。
在閱讀本文之前,應對微信公眾平台的官方開發文檔有所了解,知道接收和發送的都是xml格式的數據。另外,在做內容回復時用到了圖靈機器人的api介面,這是一個自然語言解析的開放平台,可以幫我們解決整個微信開發過程中最困難的問題,此處不多講,下面會有其詳細的調用方式。
1.1 在登錄微信官方平台之後,開啟開發者模式,此時需要我們填寫url和token,所謂url就是我們自己伺服器的介面,用WechatServlet.java來實現,相關解釋已經在注釋中說明,代碼如下:
[java] view plain copy
package demo.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import demo.process.WechatProcess;
/**
* 微信服務端收發消息介面
*
* @author pamchen-1
*
*/
public class WechatServlet extends HttpServlet {
/**
* The doGet method of the servlet. br
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”);
/** 讀取接收到的xml消息 */
StringBuffer sb = new StringBuffer();
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is, “UTF-8”);
BufferedReader br = new BufferedReader(isr);
String s = “”;
while ((s = br.readLine()) != null) {
sb.append(s);
}
String xml = sb.toString(); //次即為接收到微信端發送過來的xml數據
String result = “”;
/** 判斷是否是微信接入激活驗證,只有首次接入驗證時才會收到echostr參數,此時需要把它直接返回 */
String echostr = request.getParameter(“echostr”);
if (echostr != null echostr.length() 1) {
result = echostr;
} else {
//正常的微信處理流程
result = new WechatProcess().processWechatMag(xml);
}
try {
OutputStream os = response.getOutputStream();
os.write(result.getBytes(“UTF-8”));
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. br
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
1.2 相應的web.xml配置信息如下,在生成WechatServlet.java的同時,可自動生成web.xml中的配置。前面所提到的url處可以填寫例如:http;//伺服器地址/項目名/wechat.do
[html] view plain copy
?xml version=”1.0″ encoding=”UTF-8″?
web-app version=”2.5″
xmlns=””
xmlns:xsi=””
xsi:schemaLocation=”
“
servlet
descriptionThis is the description of my J2EE component/description
display-nameThis is the display name of my J2EE component/display-name
servlet-nameWechatServlet/servlet-name
servlet-classdemo.servlet.WechatServlet/servlet-class
/servlet
servlet-mapping
servlet-nameWechatServlet/servlet-name
url-pattern/wechat.do/url-pattern
/servlet-mapping
welcome-file-list
welcome-fileindex.jsp/welcome-file
/welcome-file-list
/web-app
1.3 通過以上代碼,我們已經實現了微信公眾平台開發的框架,即開通開發者模式並成功接入、接收消息和發送消息這三個步驟。
下面就講解其核心部分——解析接收到的xml數據,並以文本類消息為例,通過圖靈機器人api介面實現智能回復。
2.1 首先看一下整體流程處理代碼,包括:xml數據處理、調用圖靈api、封裝返回的xml數據。
[java] view plain copy
package demo.process;
import java.util.Date;
import demo.entity.ReceiveXmlEntity;
/**
* 微信xml消息處理流程邏輯類
* @author pamchen-1
*
*/
public class WechatProcess {
/**
* 解析處理xml、獲取智能回復結果(通過圖靈機器人api介面)
* @param xml 接收到的微信數據
* @return 最終的解析結果(xml格式數據)
*/
public String processWechatMag(String xml){
/** 解析xml數據 */
ReceiveXmlEntity xmlEntity = new ReceiveXmlProcess().getMsgEntity(xml);
/** 以文本消息為例,調用圖靈機器人api介面,獲取回復內容 */
String result = “”;
if(“text”.endsWith(xmlEntity.getMsgType())){
result = new TulingApiProcess().getTulingResult(xmlEntity.getContent());
}
/** 此時,如果用戶輸入的是「你好」,在經過上面的過程之後,result為「你也好」類似的內容
* 因為最終回復給微信的也是xml格式的數據,所有需要將其封裝為文本類型返回消息
* */
result = new FormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(), xmlEntity.getToUserName(), result);
return result;
}
}
2.2 解析接收到的xml數據,此處有兩個類,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通過反射的機制動態調用實體類中的set方法,可以避免很多重複的判斷,提高代碼效率,代碼如下:
[java] view plain copy
package demo.entity;
/**
* 接收到的微信xml實體類
* @author pamchen-1
*
*/
public class ReceiveXmlEntity {
private String ToUserName=””;
private String FromUserName=””;
private String CreateTime=””;
private String MsgType=””;
private String MsgId=””;
private String Event=””;
private String EventKey=””;
private String Ticket=””;
private String Latitude=””;
private String Longitude=””;
private String Precision=””;
private String PicUrl=””;
private String MediaId=””;
private String Title=””;
private String Description=””;
private String Url=””;
private String Location_X=””;
private String Location_Y=””;
private String Scale=””;
private String Label=””;
private String Content=””;
private String Format=””;
private String Recognition=””;
public String getRecognition() {
return Recognition;
}
public void setRecognition(String recognition) {
Recognition = recognition;
}
public String getFormat() {
return Format;
}
public void setFormat(String format) {
Format = format;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getLocation_X() {
return Location_X;
}
public void setLocation_X(String locationX) {
Location_X = locationX;
}
public String getLocation_Y() {
return Location_Y;
}
public void setLocation_Y(String locationY) {
Location_Y = locationY;
}
public String getScale() {
return Scale;
}
public void setScale(String scale) {
Scale = scale;
}
public String getLabel() {
return Label;
}
public void setLabel(String label) {
Label = label;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getEventKey() {
return EventKey;
}
public void setEventKey(String eventKey) {
EventKey = eventKey;
}
public String getTicket() {
return Ticket;
}
public void setTicket(String ticket) {
Ticket = ticket;
}
public String getLatitude() {
return Latitude;
}
public void setLatitude(String latitude) {
Latitude = latitude;
}
public String getLongitude() {
return Longitude;
}
public void setLongitude(String longitude) {
Longitude = longitude;
}
public String getPrecision() {
return Precision;
}
public void setPrecision(String precision) {
Precision = precision;
}
public String getEvent() {
return Event;
}
public void setEvent(String event) {
Event = event;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
Java向微信添加商品名稱
1、首先,Java打開微店,進入到「我的微店」界面。點擊右上角「添加」進入到添加商品界面,這個界面分別是商品圖片、商品描述和商品型號。
2、我們先來看商品的圖片,在這裡注意的是,商品圖片的第一張會默認為商品的封面和描述的第一張圖片,因此,大家在選擇圖片的時候一定要注意,建議將所有照片上傳後再長按拖動調整圖片的先後順序。
3、圖片調整完畢後,在商品的詳細描述中就會依次瀏覽圖片,但有的會選擇在圖片之間添加一些文字,我們點擊需要添加文字信息的圖片,可以進行圖片的美化和描述,圖片美化由官方做了一些模板供大家選擇,圖片描述時可以添加想要說明的文字。
4、而有的為了顯示微店的美觀,也會將一些文字等素材直接做到圖片里,那樣的話直接上傳圖片,調整位置就可以了。Java向微信這樣添加商品名稱。
用Java怎麼實現微信支付
具體方法步驟:
一、準備階段:已認證微信號,且通過微信支付認證,這個可以看微信文檔,很詳細,這裡就不再重複。
二、配置授權目錄,官方推薦使用https類型的url,不知道http能不能行,個人也推薦使用https的保證不會錯。
配置授權域名
三、微信支付二次開發所需要的參數:
APP_ID,APP_KEY,PARTNER,PARTNER_KEY(AppSecret)
APP_ID和PARTNER_KEY(AppSecret)
PARTNER
APP_KEY(自行設置32位字元)
四、具體編程
1、通過頁面跳轉到確認支付頁面,其中的redirect_uri必須是配置授權目錄下的。
2、獲取到openid,再經伺服器向微信請求獲取prepay_id,封裝欄位並進行簽名後通過jsapi調起微信支付
3、測試結果
用Java怎麼實現微信支付?
具體方法步驟:
一、準備階段:已認證微信號,且通過微信支付認證,這個可以看微信文檔,很詳細,這裡就不再重複。
二、配置授權目錄,官方推薦使用https類型的url,不知道http能不能行,個人也推薦使用https的保證不會錯。
配置授權域名
三、微信支付二次開發所需要的參數:
APP_ID,APP_KEY,PARTNER,PARTNER_KEY(AppSecret)
APP_ID和PARTNER_KEY(AppSecret)
PARTNER
APP_KEY(自行設置32位字元)
四、具體編程
1、通過頁面跳轉到確認支付頁面,其中的redirect_uri必須是配置授權目錄下的。
2、獲取到openid,再經伺服器向微信請求獲取prepay_id,封裝欄位並進行簽名後通過jsapi調起微信支付
3、測試結果
如何實現java程序與微信公眾平台之間實現消息推送
java程序與微信公眾平台之間實現消息推送方法:
1、本地資料庫中存放著小程序用戶表和微信公眾號的表,下面就是向某一個小程序用戶推送微信公眾號信息
2、在小程序用戶表中任意取一個用戶A信息,用戶A的openId和unionId,通過unionId到公眾號表裡去檢索對應的A用戶微信公眾號的openId
3、在微信公眾號上選擇一個模板消息,編輯完要發送的的內容後,再請求發送模板消息的介面
關於微信公眾號不能推送的,或者推送報錯的,推送的miniprogram下的appid對應的小程序必須是已審核並發布的才可以推送。
推送軟體用極光推送,實現多種消息類型,開發者可以輕鬆地通過極光發送各個移動平台的系統通知,還可以在控制台編輯多種富文本展示模板; 極光還提供自定義消息的透傳,客戶端接到消息內容後根據自己的邏輯自由處理。
微信支付Java如何判斷回調
微信支付Java判斷回調方法:
微信支付完成支付調用的時候,在傳入的參數中有一個是執行支付完成之後結果回調的參數,這個回調函數就是微信調用這個介面來將支付成功的結果。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/196370.html