彩票项目之协议分析,封装

如果在做一个项目 有很多协议和很多请求,如果一个请求改变就会发生很多变化

就不能向平常那样生成XML

public void createXMl1() {
		// 序列化
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		// This method can only be called just after setOutput
		try {
			//生成一个XML
			serializer.setOutput(writer);
			serializer.startDocument(ConstantValue.ENCONDING, null);

			serializer.startTag(null, "message");
			serializer.startTag(null, "header");

			serializer.startTag(null, "agenterid");
			serializer.text(ConstantValue.AGENTERID);
			serializer.endTag(null, "agenterid");

			serializer.startTag(null, "agenterid");
			serializer.text(ConstantValue.AGENTERID);
			serializer.endTag(null, "agenterid");

			serializer.startTag(null, "agenterid");
			serializer.text(ConstantValue.AGENTERID);
			serializer.endTag(null, "agenterid");

			serializer.endTag(null, "header");
			serializer.startTag(null, "body");
			serializer.endTag(null, "body");
			serializer.endTag(null, "message");

			serializer.endDocument();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

这样子会造成

// 无法预估
// ①开发、维护成本高——代码冗余,极易出错
// ②交接代码:开发、沟通成本
// ③生成了五个封装协议的版本代码
// ④协议变更了


所以要协议的封装  
// 风险管理——规避风险、时间影响最小化
// 单独抽一人学习协议,抽取出一个协议封装的版本(公共) 

先看这个协议  我们需要将发送出去

<?xml version='1.0' encoding='UTF-8'?>
<message version="1.0" >

    <header>

        <agenterid>
889931
        </agenterid>

        <source>
ivr
        </source>

        <compress>
DES
        </compress>

        <messengerid>
20140406085535605203
        </messengerid>

        <timestamp>
20140406085535
        </timestamp>

        <digest>
c6ab83b6e04863d405d807e38fb16ec5
        </digest>

        <transactiontype>
12002
        </transactiontype>

        <username>
        </username>
    </header>

    <body>
cgYNf1rUkTlcXIFjWR1NnuWWflFq2u/NcJddlkks4kgc7kICRqSbaAWk+QWPylCfAC9iKz1IJN9ZzAnMpChJItFUfcJligcBtzuVxmOh21TfdMNuYG4YHA==
    </body>

</message>


我们可以分为 leaf 包括 Tagname Tagvlue 简单就是 Tagname= agenterid  Tagvlue= 889931

	// <agenterid>889931</agenterid>

接下来看leaf.java  


处理的思路
// ①包含的内容
// ②序列化xml

package com.xiaoxin.CaiPiao.net.protocal;

import org.xmlpull.v1.XmlSerializer;

/**
 * 简单叶子
 * 
 * @author Administrator
 * 
 */
public class Leaf {
/*	生成它就行了*/
	// <agenterid>889931</agenterid>
	// 处理的思路
	// ①包含的内容
	// ②序列化xml
	private String tagName;
	private String tagValue;

	// 每个叶子需要指定标签名称
	public Leaf(String tagName) {
		super();
		this.tagName = tagName;
	}

	// 处理常量
	public Leaf(String tagName, String tagValue) {
		super();
		this.tagName = tagName;
		this.tagValue = tagValue;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getTagValue() {
		return tagValue;
	}

	public void setTagValue(String tagValue) {
		this.tagValue = tagValue;
	}
	/**
	 * 序列化叶子
	 * 
	 * @param serializer
	 */
	public void serializerLeaf(XmlSerializer serializer) {
		try {
			serializer.startTag(null, tagName);
			if (tagValue == null) {
				tagValue = "";
			}
			serializer.text(tagValue);
			serializer.endTag(null, tagName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


然后 头节点的封装  head.java


package com.xiaoxin.CaiPiao.net.protocal;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.codec.digest.DigestUtils;
import org.xmlpull.v1.XmlSerializer;

import com.xiaoxin.CaiPiao.ConstantValue;




/**
 * 头结点的封装
 * 
 * @author Administrator
 * 
 */
public class Header {
	// <agenterid>889931</agenterid>
	private Leaf agenterid = new Leaf("agenterid", ConstantValue.AGENTERID);
	// <source>ivr</source>
	private Leaf source = new Leaf("source", ConstantValue.SOURCE);
	// <compress>DES</compress>
	private Leaf compress = new Leaf("compress", ConstantValue.COMPRESS);
	//
	// <messengerid>20131013101533000001</messengerid>
	private Leaf messengerid = new Leaf("messengerid");
	// <timestamp>20131013101533</timestamp>
	private Leaf timestamp = new Leaf("timestamp");
	// <digest>7ec8582632678032d25866bd4bce114f</digest>
	private Leaf digest = new Leaf("digest");
	//
	// <transactiontype>12002</transactiontype> 销售期
	private Leaf transactiontype = new Leaf("transactiontype");
	// <username>13200000000</username>用户名
	private Leaf username = new Leaf("username");

	/**
	 * 序列化头
	 * 
	 * @param serializer
	 * @param body
	 *            完整的body(明文)
	 */
	public void serializerHeader(XmlSerializer serializer, String body) {
		// 将timestamp、messengerid、digest设置数据
		//设置时间戳
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = format.format(new Date());
		timestamp.setTagValue(time);
		// messengerid:时间戳+六位的随机数
		Random random = new Random();
		int num = random.nextInt(999999) + 1;// [0, n).[1,999999]
		DecimalFormat decimalFormat = new DecimalFormat("000000");
		messengerid.setTagValue(time + decimalFormat.format(num));
		// digest:时间戳+代理商的密码+完整的body(明文)
		String orgInfo = time + ConstantValue.AGENTER_PASSWORD + body;
		String md5Hex = DigestUtils.md5Hex(orgInfo);
		digest.setTagValue(md5Hex);
		try {
			//将数据发送到leaf生成XML
			serializer.startTag(null, "header");
			agenterid.serializerLeaf(serializer);
			source.serializerLeaf(serializer);
			compress.serializerLeaf(serializer);

			messengerid.serializerLeaf(serializer);
			timestamp.serializerLeaf(serializer);
			digest.serializerLeaf(serializer);

			transactiontype.serializerLeaf(serializer);
			username.serializerLeaf(serializer);

			serializer.endTag(null, "header");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Leaf getTransactiontype() {
		return transactiontype;
	}

	public Leaf getUsername() {
		return username;
	}

	/********************* 处理服务器回复 *************************/
	public Leaf getTimestamp() {
		return timestamp;
	}

	public Leaf getDigest() {
		return digest;
	}

}

漏说一点 主要是 tagValue需要动态获取

这个两个变量都要在后面获取改变的 所以要将值返回出去

transactiontype

username

public Leaf getTransactiontype() {
		return transactiontype;
	}


	public Leaf getUsername() {
		return username;
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值