热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

这篇文章讲解真正的网络请求类HttpClient,它负责打开网络链接,获取数据,把数据存放于一个字节数组,让Response

这篇文章讲解真正的网络请求类HttpClient,它负责打开网络链接,获取数据,把数据存放于一个字节数组,让Response解析。

以下是代码:

package app.http; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import app.program.GameConst; /** * 功能&#xff1a; * * 打开网络链接&#xff0c;获取数据&#xff0c;把数据存放于一个字节数组&#xff0c;让Response解析 */ public class HttpClient { // 注: HttpsConnection继承自HttpConnection private HttpConnection conn &#61; null; private InputStream inputStream &#61; null; private OutputStream outputStream &#61; null; private Request request &#61; null; int msgId; public HttpClient(int _msgId) { msgId &#61; _msgId; } public Response getResponse(Request request) throws Exception { if (request &#61;&#61; null) { return null; } try { this.request &#61; request; Response resp &#61; new Response(); // 打开链接 makeConnection(); // 设置参数 setProperty(); // 写入内容 setContent(); // 打开输入流 makeInputStream(); // 得到响应码 resp.responseCode &#61; conn.getResponseCode(); if (resp.responseCode !&#61; HttpConnection.HTTP_OK) { return null; } String strContentLength &#61; conn.getHeaderField("content-length"); if (strContentLength !&#61; null) { resp.contentLength &#61; Integer.parseInt(strContentLength); } resp.analysisData(readResponse(), true); return resp; } catch (Exception e) { e.printStackTrace(); throw e; } finally { this.request &#61; null; cleanup(); } } private void makeConnection() throws Exception { conn &#61; (HttpConnection) Connector.open("http://", Connector.READ_WRITE, true); } private void makeInputStream() throws Exception { inputStream &#61; conn.openInputStream(); } private void makeOutputStream() throws Exception { outputStream &#61; conn.openOutputStream(); } /** * 参数设置 * * &#64;throws Exception */ private void setProperty() throws Exception { conn.setRequestMethod(HttpConnection.POST); } public void setContent() throws Exception { if (request.getContent() !&#61; null) { conn.setRequestProperty("Content-Length", String.valueOf(request.getContent().length)); makeOutputStream(); outputStream.write(request.getContent()); } } /** * 从网络流中读取数据&#xff0c;存放于字节数组中 * * &#64;return * &#64;throws Exception */ private byte[] readResponse() throws Exception { try { byte[] data &#61; null; if (conn !&#61; null && inputStream !&#61; null) { int len &#61; (int) conn.getLength(); // 读取模式1&#xff1a;直接获取数据长度 if (conn.getRequestMethod().equals(HttpConnection.HEAD)) { return null; } if (len <&#61; 0) { // 读取模式2&#xff1a;从Http连接头获取数据长度 String bodyLength &#61; null; try { bodyLength &#61; conn.getHeaderField("content-length"); } catch (IOException e) { e.printStackTrace(); } if (bodyLength !&#61; null) { try { len &#61; Integer.parseInt(conn.getHeaderField("Body-Length")); } catch (Exception e) { e.printStackTrace(); } } } if (len > 0) { int actual &#61; 0; int bytesread &#61; 0; data &#61; new byte[len]; while ((bytesread !&#61; len) && (actual !&#61; -1)) { actual &#61; inputStream.read(data, bytesread, len - bytesread); if (actual >&#61; 0) { bytesread &#43;&#61; actual; } } if (bytesread &#61;&#61; 0) { return null; } else if (bytesread
重点关注方法是&#xff1a;

public Response getResponse(Request request) throws Exception&#xff1b;

代码里面涉及了Response和StructResponse&#xff0c;它们的代码页一并罗列&#xff1a;

package app.http; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.EOFException; import java.io.IOException; import java.util.Hashtable; import java.util.Vector; import app.program.Globe; /** * 返回信息的解析方法类&#xff0c;和StructRequest对应 */ public class StructResponse { private DataInputStream in; private ByteArrayInputStream bin; private Hashtable hash; private Vector vec; private int size; public StructResponse(byte[] data) { bin &#61; new ByteArrayInputStream(data); in &#61; new DataInputStream(bin); } public StructResponse(byte[] data, boolean bTradeCipher) { hash &#61; new Hashtable(); vec &#61; new Vector(); if (!bTradeCipher) { readTradeData(data); } else { readTradeCipherData(data); } this.getTable(); } private boolean bTable &#61; false; private int tableIndex &#61; 0; // 表格数据的列数 private void readTradeData(byte[] data) { int i &#61; 0; try { for (; i 0) { String s &#61; new String(data, 0, i, "UTF-8"); putInHash(s); } break; } else if (data[i] &#61;&#61; 0x04) { bTable &#61; false; String s &#61; new String(data, 0, i, "UTF-8"); vec.addElement(s); if (tableIndex &#61;&#61; 0) { tableIndex &#61; vec.size(); } break; } } String s &#61; new String(data, 0, i, "UTF-8"); putInHash(s); } catch (Exception ex) { ex.printStackTrace(); } if (data.length - i - 1 > 0) { byte[] nData &#61; new byte[data.length - i - 1]; System.arraycopy(data, i &#43; 1, nData, 0, data.length - i - 1); readTradeData(nData); } } private void readTradeCipherData(byte[] data) { for (int i &#61; 0; i &#61; 0) { String s1 &#61; s.substring(0, index); String s2 &#61; s.substring(index &#43; 1, s.length()); this.hash.put(s1, s2); } } public String getHashData(String key) { return (String) hash.get(key); } public String[][] getTable() { if (tableIndex <&#61; 0) { return null; } int line &#61; vec.size() / this.tableIndex; String[][] tmp &#61; new String[line][tableIndex]; for (int i &#61; 0; i

package app.http; import java.util.Hashtable; import java.io.*; /** * 按照私有协议&#xff0c;解析数据&#xff0c;和Request对应&#xff0c;把最终解析结果存放于一个hash中&#xff0c;存放格式是 消息码--消息内容 */ public class Response { private Hashtable hash &#61; new Hashtable(); public int responseCode; public int contentLength; private static final byte START_FLAG &#61; (byte) &#39;{&#39;; private static final byte END_FLAG &#61; (byte) &#39;}&#39;; private static final byte OTHER_FLAG &#61; (byte) &#39;:&#39;; public Response() { } /** * 分析数据 解析TYPE和真正数据content * * &#64;param data * byte[] */ public void analysisData(byte[] _data, boolean isStart) { StructResponse struct &#61; new StructResponse(_data); byte[] data &#61; null; try { while (isStart) { int v &#61; struct.readByte(); if (v &#61;&#61; -1) { throw new IOException(" DATA"); } if (v &#61;&#61; START_FLAG) { break; } } int type &#61; struct.readShort(); struct.readByte(); // 0 标志位 struct.readByte(); // 0 标志位 data &#61; struct.readByteArray(); byte tmp &#61; (byte) struct.readByte(); if (tmp &#61;&#61; END_FLAG) { hash.put(Integer.toString(type), data); } else if (tmp &#61;&#61; OTHER_FLAG) { hash.put(Integer.toString(type), data); analysisData(struct.getOthers(), false); } else { throw new IOException("BAD DATA"); } } catch (IOException ex) { ex.printStackTrace(); } } public int getResponseCode() { return responseCode; } public int getContentLength() { return contentLength; } public byte[] getData(int key) { return (byte[]) hash.get(Integer.toString(key)); } public int getResponseNum() { return hash.size(); } }




推荐阅读
author-avatar
趣校区导购网
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有