第一节讲了怎么准备所有的参数,这里讲怎么将这些参数组成xml(即要通过HTTPS发送的数据)大家可以先在网上了解一下java bean转xml的相关知识
1、我建了里一个请求参数类
public class WechatCashHongbaoSendReq {
private String nonce_str; //随机字符串
private String sign; //签名
private String mch_billno; //商户订单号
private String mch_id; //商户号
private String wxappid; //公众账号appid
private String send_name; //商户名称
private String re_openid; //用户openid
private int total_amount; //付款金额
private int total_num; //红包发放总人数
private String wishing; //红包祝福语
private String client_ip; //Ip地址
private String act_name; //活动名称
private String remark; //备注
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getMch_billno() {
return mch_billno;
}
public void setMch_billno(String mch_billno) {
this.mch_billno = mch_billno;
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public String getWxappid() {
return wxappid;
}
public void setWxappid(String wxappid) {
this.wxappid = wxappid;
}
public String getSend_name() {
return send_name;
}
public void setSend_name(String send_name) {
this.send_name = send_name;
}
public String getRe_openid() {
return re_openid;
}
public void setRe_openid(String re_openid) {
this.re_openid = re_openid;
}
public int getTotal_amount() {
return total_amount;
}
public void setTotal_amount(int total_amount) {
this.total_amount = total_amount;
}
public int getTotal_num() {
return total_num;
}
public void setTotal_num(int total_num) {
this.total_num = total_num;
}
public String getWishing() {
return wishing;
}
public void setWishing(String wishing) {
this.wishing = wishing;
}
public String getClient_ip() {
return client_ip;
}
public void setClient_ip(String client_ip) {
this.client_ip = client_ip;
}
public String getAct_name() {
return act_name;
}
public void setAct_name(String act_name) {
this.act_name = act_name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
2、然后把13个参数都set进去
WechatCashHongbaoSendReq sendReq = new WechatCashHongbaoSendReq();
sendReq.setNonce_str(nonce_str);
sendReq.setSign(sign);
sendReq.setMch_billno(mch_billno);
sendReq.setMch_id(mch_id);
sendReq.setWxappid(wxappid);
sendReq.setSend_name(send_name);
sendReq.setRe_openid(re_openid);
sendReq.setTotal_amount(total_amount);
sendReq.setTotal_num(total_num);
sendReq.setWishing(wishing);
sendReq.setClient_ip(client_ip);
sendReq.setAct_name(act_name);
sendReq.setRemark(remark);
3、将请求类转为xml
String data = Util.bean2XML(req).replaceAll("__", "_"); //为什么要加一个replaceAll("__", "_"),如果不加,会有什么情况,大家可以试试
写一个Util类,代码如下:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import java.io.*;
import java.lang.reflect.Field;
import java.util.Map;
/**
* User: rizenguo
* Date: 2014/10/23
* Time: 14:59
*/
public class Util {
//打log用
/**
* 通过反射的方式遍历对象的属性和属性值,方便调试
*
* @param o 要遍历的对象
* @throws Exception
*/
public static void reflect(Object o) throws Exception {
Class cls = o.getClass();
Field[] fields = cls.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
f.setAccessible(true);
Util.log(f.getName() + " -> " + f.get(o));
}
}
public static byte[] readInput(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.close();
in.close();
return out.toByteArray();
}
public static String inputStreamToString(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();
}
public static InputStream getStringStream(String sInputString) throws UnsupportedEncodingException {
ByteArrayInputStream tInputStringStream = null;
if (sInputString != null && !sInputString.trim().equals("")) {
tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("UTF-8"));
}
return tInputStringStream;
}
public static Object getObjectFromXML(String xml, Class tClass) {
//将从API返回的XML数据映射到Java对象
xstream.alias("xml", tClass);
return xstream.fromXML(xml);
}
public static String getStringFromMap(Map<String, Object> map, String key, String defaultValue) {
if (key == "" || key == null) {
return defaultValue;
}
String result = (String) map.get(key);
if (result == null) {
return defaultValue;
} else {
return result;
}
}
public static int getIntFromMap(Map<String, Object> map, String key) {
if (key == "" || key == null) {
return 0;
}
if (map.get(key) == null) {
return 0;
}
return Integer.parseInt((String) map.get(key));
}
/**
* 打log接口
* @param log 要打印的log字符串
* @return 返回log
*/
public static String log(Object log){
return log.toString();
}
/**
* 读取本地的xml数据,一般用来自测用
* @param localPath 本地xml文件路径
* @return 读到的xml字符串
*/
public static String getLocalXMLString(String localPath) throws IOException {
return Util.inputStreamToString(Util.class.getResourceAsStream(localPath));
}
/**
* 将java对象转为xml字符串
* @param clazz
* @return
*/
public static String bean2XML(Object o){
xstream.alias("xml", o.getClass());
return xstream.toXML(o);
}
/**
* 扩展xstream,使其支持CDATA块
*
* @date 2013-05-19
*/
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
@SuppressWarnings("unchecked")
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
}
到这一步,已经完成了最终要发送出去的数据,然后是写http请求方法。
声明:工具类代码参考微信支付开发的demo,有些自己做了小改动。
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>