Messagepack use of Android data transfer

Source: Internet
Author: User

After describing what is Messagepack, use Android and Messagepack.

On Messagepack's official web site, Messagepack and Java are used in conjunction with MAVEN as Jar management, and it's inconvenient to be familiar with Maven, because it's not familiar, (if you want to continue to be familiar with Maven's friends, Recommend a MAVEN address: http://mvnrepository.com/)

I'd like to tidy up the process of using Messagepack in Android development from request to response:

(1) To use the four packages that Messagepack needs to use, a msgpack address is required to query through the recommended MAVEN address: http://mvnrepository.com/artifact/org.msgpack/msgpack/ 0.6.7, for friends who are familiar with Maven, can find:

Using Msgpack will also depend on the other three jars, where only junit is the jar that can be downloaded, and the other json-simple,javassist,msgpack need to be packaged by themselves, and I have packaged them in the following versions:

jar:http://download.csdn.net/detail/yddido/5725799

(2) Introduce the encapsulation of the class that I usually develop in the network request:

(3) Start using Msgpack--request request parameter: (Will request data pack)

[Java]View Plaincopy
  1. Messagepack msgpack = new Messagepack ();
  2. byte[] outbytes = null;
  3. Bytearrayoutputstream out = new Bytearrayoutputstream ();
  4. Packer Packer = Msgpack.createpacker (out);
  5. Map postdata = new HashMap ();
  6. Postdata.put ("SId", data);
  7. try {
  8. Packer.write (PostData);
  9. Outbytes = Out.tobytearray ();
  10. } catch (IOException e) {
  11. E.printstacktrace ();
  12. }

(4) How to request the network in handler:

[Java]View Plaincopy
  1. Byte[] result = null;
  2. HttpClient HttpClient = new Defaulthttpclient ();
  3. Httpclient.getparams (). Setparameter (
  4. Coreconnectionpnames.connection_timeout, 1000);
  5. Httpclient.getparams (). Setparameter (Coreconnectionpnames.so_timeout,
  6. Ten * 1000);
  7. HttpPost HttpPost;
  8. HttpPost = new HttpPost (URL);
  9. try {
  10. Multipartentity mpentity = new Multipartentity ();
  11. Bytearraybody databody = new Bytearraybody (Outbytes, "Memory");
  12. Mpentity.addpart (ParamName, databody);
  13. Httppost.setentity (mpentity);
  14. HttpResponse response = Httpclient.execute (HttpPost);
  15. if (Response.getstatusline (). Getstatuscode ()! = 404) {
  16. result = Entityutils.tobytearray (response.getentity ());
  17. }
  18. } catch (Exception e) {
  19. LOG.E ("Httputils", "Connection server error");
  20. E.printstacktrace ();
  21. }

At this point, the binary data is requested to the network and responds to the binary data stream

(5) The remaining steps will be obtained by using the Msgpack-unpack method to obtain the binary data you want to obtain the corresponding data:

[Java]View Plaincopy
  1. Value A;
  2. try {
  3. A = (new Messagepack ()). Read (result);
  4. System.out.println ("ADF");
  5. } catch (IOException e) {
  6. E.printstacktrace ();
  7. }

In the data transmission certainly has the more complex data structure the use, once again simply introduced the Msgpack the use method, if wants the thorough understanding also to need more practice.

Msgpack also has its own official Wiki introduction: Http://wiki.msgpack.org/display/MSGPACK/QuickStart+for+Java

If you have any questions, study progress together!

Messagepack use of Android data transfer

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.