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
- 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) How to request the network in handler:
[Java]View Plaincopy
- Byte[] result = null;
- HttpClient HttpClient = new Defaulthttpclient ();
- Httpclient.getparams (). Setparameter (
- Coreconnectionpnames.connection_timeout, 1000);
- Httpclient.getparams (). Setparameter (Coreconnectionpnames.so_timeout,
- Ten * 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", "Connection server error");
- E.printstacktrace ();
- }
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
- Value A;
- try {
- A = (new Messagepack ()). Read (result);
- System.out.println ("ADF");
- } catch (IOException e) {
- E.printstacktrace ();
- }
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