Android 数据传输之MessagePack使用
介绍过什么是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-易做图,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的使用方法,如果想深入了解还需要多多实践。
补充:移动开发 , Android ,