After introducing MessagePack, we will use Android and MessagePack.
On the official website of MessagePack, we will introduce that MessagePack and Java are used in combination with Maven for JAR management. Since Maven is not familiar with configuration, it is really inconvenient to get familiar with it, (If you want to continue to be familiar with maven, we recommend a maven address: http://mvnrepository.com /)
I want to sort out the process of using MessagePack in Android development from request to response:
(1) To use MessagePack needs to use the four packages, through the recommended maven address query needs to use msgpack address: http://mvnrepository.com/artifact/org.msgpack/msgpack/0.6.7, for familiar maven friends can find:
When msgpack is used, the other three jar files are dependent. Only junit jar files can be downloaded. Other json-simple, javassist, and msgpack files must be packaged by myself, the versions are as follows:
(2) introduce the encapsulation of network request classes in your usual development:
(3) start to use msgpack -- request parameters: (pack the request data)
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) method for requesting network in 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.exe cute (httppost); if (response. getStatusLine (). getStatusCode ()! = 404) {result = EntityUtils. toByteArray (response. getEntity () ;}} catch (Exception e) {Log. e ("HttpUtils", "server connection error"); 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 httpp Ost; 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.exe cute (httppost); if (response. getStatusLine (). getStatusCode ()! = 404) {result = EntityUtils. toByteArray (response. getEntity () ;}} catch (Exception e) {Log. e ("HttpUtils", "server connection error"); e. printStackTrace ();}
In this case, the binary data is requested to the network and the data stream to the binary data is returned.
(5) In the remaining steps, you can use the msgpack-unpack method to obtain the binary data you want:
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(); }
There must be a more complex use of data structures in data transmission. Once again, it is just a brief introduction to the use of msgpack. If you want to know more about it, you need more practices.