}
}
public void cloese() {
try {
if (out != null) {
out.close();
}
if (bout != null) {
bout.close();
}
out = null;
bout = null;
} catch (IOException ex) {
}
}
}
下面罗列Request的代码,注意里面的请求方式分为两种,第一种是一次只请求一种数据,第二种是一次请求多种数据(这种方式有效的减少了请求的次数):
view plaincopy to clipboardprint package app.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* 功能:
*
* 把需要发送的内容,再次经过私有协议封装,合并为一个Request对象,并且给这个对象一个和屏幕一致的ID,用于接收和解析数据
*
* 其他参照StructRequest
*/
public class Request {
private byte[] content = null;
private int screenId = 0;
private static final byte START_FLAG = (byte) '{';
private static final byte END_FLAG = (byte) '}';
private static final byte OTHER_FLAG = (byte) ':';
private boolean bTrade = false;
/**
* 单个通讯
*
* @param output
* StructOutput
*/
public Request(StructRequest output, int screeID) {
screenId = screeID;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
out.write(START_FLAG); // 标识符
out.write(output.getType() & 0xFF);
out.write((output.getType() >>> 8) & 0xFF);
out.write(0);
out.write(0);
out.write(output.getBytes().length & 0xFF);
out.write((output.getBytes().length >>> 8) & 0xFF);
out.write(output.getBytes());
out.write(END_FLAG); // 校验符
} catch (Exception ex) {
}
content = out.toByteArray();
try {
out.close();
} catch (IOException ex1) {
}
}
public Request(int screeID) {
screenId = screeID;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
out.write(START_FLAG); // 标识符
out.write(0);
out.write(0);
out.write(0);
out.write(0);
out.write(0);
out.write(0);
out.write(END_FLAG); // 校验符
} catch (Exception ex) {
}
content = out.toByteArray();
try {
out.close();
} catch (IOException ex1) {
}
out = null;
}
/**
* 多个通讯
*
* @param output
* StructOutput[]
*/
public Request(StructRequest[] output, int screeID) {
screenId = screeID;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
out.write(START_FLAG); // 标识符
for (int i = 0; i < output.length; i++) {
out.write(output[i].getType() & 0xFF);
out.write((output[i].getType() >>> 8) & 0xFF);
out.write(0);
out.write(0);
out.write(output[i].getBytes().length & 0xFF);
out.write((output[i].getBytes().length >>> 8) & 0xFF);
out.write(output[i].getBytes());
if (i < output.length - 1) {
out.write(OTHER_FLAG);
}
}
out.write(END_FLAG); // 校验符
} catch (Exception ex) {
}
content = out.toByteArray();
try {
out.close();
} catch (IOException ex1) {
}
out = null;
}
public int getScreenId() {
return screenId;
}