介紹過什麼是MessagePack之後,就進行Android與MessagePack的使用。
在MessagePack的官網上介紹MessagePack與Java結合使用的都是使用Maven作為JAR管理進行,由於不熟悉Maven,配置起來還有熟悉起來確實感覺很不方便,(如果想繼續熟悉使用maven的朋友,推薦一個maven的地址:http://mvnrepository.com/)
我想整理一下,我從請求到響應在Android開發中使用MessagePack的過程:
(1)想要使用MessagePack需要使用的四個包,通過推薦的maven地址查詢需要使用msgpack地址:http://mvnrepository.com/artifact/org.msgpack/msgpack/0.6.7, 對於熟悉maven的朋友說可以發現下圖:
使用msgpack的同時會依賴瞭另外三個jar,其中隻有junit是可以下載的jar,其他的json-simple,javassist,msgpack都需要自行打包,我已經打包好,分別是版本如下:
(2)介紹一下自己平時開發中對網絡請求的類的封裝:
(3)開始使用msgpack–request請求參數:(將請求數據pack)
MessagePack msgPack = new MessagePack(); byte[] outbytes = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = msgPack.createPacker(out); Map postData = new HashMap(); postData.put("SId", data); try { packer.write(postData); outbytes = out.toByteArray(); } catch (IOException e) { e.printStackTrace(); } MessagePack msgPack = new MessagePack(); byte[] outbytes = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = msgPack.createPacker(out); Map postData = new HashMap(); postData.put("SId", data); try { packer.write(postData); outbytes = out.toByteArray(); } catch (IOException e) { e.printStackTrace(); }
(4)handler中請求網絡的方法:
byte[] result = null; HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 1000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000); HttpPost httppost; httppost = new HttpPost(url); try { MultipartEntity mpEntity = new MultipartEntity(); ByteArrayBody dataBody = new ByteArrayBody(outbytes, "memory"); mpEntity.addPart(paramName, dataBody); httppost.setEntity(mpEntity); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() != 404) { result = EntityUtils.toByteArray(response.getEntity()); } } catch (Exception e) { Log.e("HttpUtils", "連接服務器出錯"); e.printStackTrace(); } byte[] result = null; HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 1000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000); HttpPost httppost; httppost = new HttpPost(url); try { MultipartEntity mpEntity = new MultipartEntity(); ByteArrayBody dataBody = new ByteArrayBody(outbytes, "memory"); mpEntity.addPart(paramName, dataBody); httppost.setEntity(mpEntity); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() != 404) { result = EntityUtils.toByteArray(response.getEntity()); } } catch (Exception e) { Log.e("HttpUtils", "連接服務器出錯"); e.printStackTrace(); }
此時,請求二進制的數據至網絡,並且響應到二進制的數據流
(5)剩下的步驟,將獲取到的二進制數據使用msgpack- unpack方法就可以獲取到自己想要獲取相應的數據:
Value a; try { a = (new MessagePack()).read(result); System.out.println("adf"); } catch (IOException e) { e.printStackTrace(); } Value a; try { a = (new MessagePack()).read(result); System.out.println("adf"); } catch (IOException e) { e.printStackTrace(); }
在數據傳遞中肯定還有較為復雜的數據結構的使用,再次隻是簡單的介紹瞭msgpack的使用方法,如果想深入瞭解還需要多多實踐。