Java – using messagepack with Android

Has anyone tried to use @ l_ 502_ 0 @ app using messagepack?

Caused by: java.lang.ExceptionInInitializerError
  at org.msgpack.Packer.pack(Packer.java:532)
  at org.msgpack.MessagePack.pack(MessagePack.java:31)
  ... 15 more
  Caused by: java.lang.ExceptionInInitializerError
  at org.msgpack.template.TemplateRegistry.<clinit>(TemplateRegistry.java:38)
  ... 17 more
  Caused by: java.lang.VerifyError: org.msgpack.template.BeansFieldEntryReader
  at org.msgpack.template.builder.BeanstemplateBuilder.<init (BeanstemplateBuilder.java:42)
  at org.msgpack.template.builder.BuilderSelectorRegistry.initForJava(BuilderSelectorRegistry.java:73)
  at org.msgpack.template.builder.BuilderSelectorRegistry.<clinit>(BuilderSelectorRegistry.java:38)
  ... 18 more

The code I use is very simple

PrintWriter out = new PrintWriter(socket.getOutputStream());
Message msg = new Message();
msg.body = "asdasdasd";
msg.from = "qwe";
msg.to = "ttt";
byte[] bytes = MessagePack.pack(msg);
out.print(bytes);
out.flush();

My lib directory has javassist jar,msgpack-0.5. 2.jar,slf4j-api-1.6. 2. Jar and slf4j-jdk14-1.6 2.jar.

In my server application, this code is normal for the same library

Solution

(hope) last update

msgpack:0.6. 8 no problems on Android

msgpack-rpc:0.7. 0 has a warning on Android

Specifically, you need to add the following content to oncreate of API level 8 (Android 2.2.1), which may be lower:

java.lang.System.setProperty("java.net.preferIPv4Stack","true");
java.lang.System.setProperty("java.net.preferIPv6Addresses","false");

Because of this bug

If you want to see a simple example, here is a project designed for this purpose:

> https://github.com/mikkoz/msgpack-android-test-server/tree/master/msgpack-android-test-server > https://github.com/mikkoz/msgpack-android-test-client/tree/master/msgpack-android-test-client

Previous versions

Update: from 0.6 7 msgpack should be compatible with Android Please check msgpack RPC in the following text (it may be modified later)

Note: if you also use msgpack RPC, you need to perform the following steps:

>From git: / / GitHub com/msgpack/msgpack-rpc. Git (specifically, "Java" folder) Download msgpack RPC source code. > Change the master msgpack artifact version to the version you have built. > At org msgpack. rpc. loop. netty. In nettyeventloop, change nioclientsocketchannelfactory to ooclientsocketchannelfactory (getworkerexecution()). > Build messagepack - RPC in the same way as the main messagepack jar (see step 11 above)

Nettyeventloop replacement is due to this problem: http://markmail.org/message/ypa3nrr64kzsyfsa.

Important: I only tested synchronous communication Asynchrony may not work

And this is msgpack at 0.6 7. Reasons why Android could not be used before:

The reason for the error is that the messagepack uses several Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java. Java Beans class You may be using the messagepackbeans annotation

This is similar to the problem described here, which outlines the general solution of here Unfortunately, in our example, it needs to rebuild msgpack That's what I did (I almost certainly skipped steps 5 and 8, but I didn't try)

>From https://github.com/msgpack/msgpack-java.git Download messagepack source code. > Import the messagepack source into a project in the IDE. > from http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java Download Apache harmony source code of relevant software packages. > Copy these packages to the Src / main / Java folder of the messagepack project:

> java. beans > java. beans. beancontext > org. apache. harmony. beans > org. apache. harmony. beans. internal. nls

>In the messagepack project, delete the following classes:

> Propertychangelistener > IndexedPropertyChangeEvent > PropertyChangeEvent > PropertychangelistenerProxy > PropertyChangeSupport

>Add Java Rename the beans package to a different package, such as custom beans. > Put all Java The reference of beans is changed to the renamed ID, custom beans. This applies especially to beansfieldentryreader (this class is the cause of the original error). > The custom. Of the five classes you deleted in step 5 The beans reference is changed back to Java beans. > At org apache. harmony. beans. internal. nls. In the messages class, comment out the method setlocale and delete the import associated with it. > All classes that still have errors except encoder In this class, comment out all references of all classes you deleted You should now have an error free project. > Build messagepack jar:

>If you use maven, put POM Change the version in XML to a unique version, then run Maven build with the installation target, and then use this version to add dependencies in the Android project. > If you do not use maven, you must use the included build XML to run ant's jar target Replace msgpack jar with your Android project

>If you are publishing an application, remember to include the relevant legal statements of Apache harmony This is an Apache license, just like message pack

This should be done. Using sample code and my own data classes, I can successfully package and decompress the data

The whole renaming ceremony was due to the DEX compiler complaining about Java* The fact that packages are named

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>