当前位置:编程学习 > JAVA >>

移动开发中的通信架构(二)

先罗列一小段代码,这一小段代码代表了通信框架中发送请求的基本方式,读者可以根据这段代码,分析整个请求过程:

view plaincopy to clipboardprint?/**
 * 典型使用方式:
 * 
 * StructRequest output = new StructRequest(GameConst.COMM_PRICECOUNT_DATA);//GameConst.COMM_PRICECOUNT_DATA是请求ID,作用有两个:用于服务器端识别请求,用于UI线程解析数据
 * 
 * output.writeString("");//请求数据
 * 
 * StructRequest output_time = new StructRequest(GameConst.COMM_SERVER_TIME);
 * 
 * output_time.writeInt(0);
 * 
 * StructRequest[] temp = { output, output_time };
 * 
 * Request request = new Request(temp, this.screenId);//依靠Request对象封装为请求
 * 
 * sendRequest(request);//发送请求{这里是耦合点,注意,发送请求的时候,程序的控制类[UI和网络的耦合类]会记录下请求的屏幕ID,用于解析}
 * 
 * output.cloese();//关闭读写
 */ 
/**
 * 典型使用方式:
 *
 * StructRequest output = new StructRequest(GameConst.COMM_PRICECOUNT_DATA);//GameConst.COMM_PRICECOUNT_DATA是请求ID,作用有两个:用于服务器端识别请求,用于UI线程解析数据
 *
 * output.writeString("");//请求数据
 *
 * StructRequest output_time = new StructRequest(GameConst.COMM_SERVER_TIME);
 *
 * output_time.writeInt(0);
 *
 * StructRequest[] temp = { output, output_time };
 *
 * Request request = new Request(temp, this.screenId);//依靠Request对象封装为请求
 *
 * sendRequest(request);//发送请求{这里是耦合点,注意,发送请求的时候,程序的控制类[UI和网络的耦合类]会记录下请求的屏幕ID,用于解析}
 *
 * output.cloese();//关闭读写
 */
下面罗列StructRequest 类代码,代码中有详细注释,就不多叙述了。

view plaincopy to clipboardprint?package app.http; 
 
import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.util.Vector; 
 
import app.program.Globe; 
 
/**
 * 功能:把各种类型的数据,转换为字节数组。
 * 
 * 属于工具类,为网络连接,本地数据存储提供从其他数据类型到字节数据类型的转换。
 * 
 * 注意有些方法中包含了私有项目的网络协议,并没有删除。比如writeInts2中writeLength(v[0].length);
 */ 
public class StructRequest { 
    private ByteArrayOutputStream bout; 
 
    private DataOutputStream out; 
 
    private int commID; 
 
    private boolean bTradeCipher; 
 
    /**
     * 默认为非加密通讯
     * 
     * @param commID
     *            int
     */ 
    public StructRequest(int commID) { 
        this(); 
        this.commID = commID; 
        bTradeCipher = false; 
    } 
 
    /**
     * @param commID
     *            int
     * @param bTradeCipher
     *            boolean 表示是否需要加密
     */ 
    public StructRequest(int commID, boolean bTradeCipher) { 
        this(); 
        this.commID = commID; 
        this.bTradeCipher = bTradeCipher; 
    } 
 
    public StructRequest() { 
        bout = new ByteArrayOutputStream(); 
        out = new DataOutputStream(bout); 
    } 
 
    public void writeLength(int v) { 
        writeShort(v); 
    } 
 
    public void writeBoolean(boolean v) { 
        try { 
            out.writeBoolean(v); 
        } catch (Exception ex) { 
            throw new RuntimeException(); 
        } 
    } 
 
    public void writeBooleans(boolean[] v) { 
        writeLength(v.length); 
        for (int i = 0; i < v.length; i++) { 
            writeBoolean(v[i]); 
        } 
    } 
 
    public void writeBooleans2(boolean[][] v) { 
        writeLength(v.length); 
        if (v.length > 0) { 
            writeLength(v[0].length); 
            for (int i = 0; i < v.length; i++) { 
                for (int j = 0; j < v[0].length; j++) { 
                    writeBoolean(v[i][j]); 
                } 
            } 
        } 
    } 
 
    public void writeByte(int v) { 
        try { 
            out.writeByte(v); 
        } catch (Exception ex) { 
            throw new RuntimeException(); 
        } 
    } 
 
    public void writeBytes(int[] v) { 
        writeLength(v.length); 
        for (int i = 0; i < v.length; i++) { 
            writeByte(v[i]); 
        } 
    } 
 
    public void writeBytes2(in

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,