介紹過什麼是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的使用方法,如果想深入瞭解還需要多多實踐。