MessagePack for Android Data Transmission

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.