使用MessagePack与Android [英] Using MessagePack with Android

查看:893
本文介绍了使用MessagePack与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人曾经试图用 MessagePack 有一个Android应用程序?
可能吗?我曾尝试使用从 msgpack-Java中的Jar 并接受以下异常:

Has someone tried to use MessagePack with an Android app?
Is it possible? I have tried to use the Jar from msgpack-java and received the following Exception:

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

这是我使用的code是很简单的。

The code that 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();

我有 javassist.jar msgpack-0.5.2.jar SLF4J在我的 LIB SLF4J-jdk14-1.6.2.jar C $ C>目录。

I have javassist.jar, msgpack-0.5.2.jar, slf4j-api-1.6.2.jar and slf4j-jdk14-1.6.2.jar in my lib directory.

在我的服务器应用程序这个code正常工作与相同的库。

In my server application this code works fine with the same libraries.

推荐答案

msgpack 0.6.8 工作在Android上没有任何问题。

(Hopefully) FINAL UPDATE

msgpack : 0.6.8 works on Android without any problems

msgpack-RPC 0.7 0.0 在Android上有一点需要注意的作品。

msgpack-rpc : 0.7.0 works on Android with one caveat.

具体而言,则需要以下内容添加到的onCreate 的API等级8(安卓2.2.1),并有可能降低:

Specifically, you need to add the following to onCreate for API Level 8 (Android 2.2.1), and possibly lower:

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

由于这个bug

如果你想看到一个简单的例子,这里有一个对设立为此项目:

If you want to see a simple example, here's a pair of projects set up for this purpose:

  • <一个href="https://github.com/mikkoz/msgpack-android-test-server/tree/master/msgpack-android-test-server">https://github.com/mikkoz/msgpack-android-test-server/tree/master/msgpack-android-test-server
  • <一个href="https://github.com/mikkoz/msgpack-android-test-client/tree/master/msgpack-android-test-client">https://github.com/mikkoz/msgpack-android-test-client/tree/master/msgpack-android-test-client
  • 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

更新:由于0.6.7 msgpack应与兼容Android的(有一个小的依赖性排斥的问题)。检查文字下面msgpack-RPC(这也可能会在未来进行调整)。

UPDATE: as of 0.6.7 msgpack should be compatible with Android (there is a small dependency exclusion issue). Check the text below for msgpack-rpc (which also might be adapted in the future).

注意:如果您还使用的 msgpack-RPC 的,你需要做以下步骤:

NOTE: If you're also using msgpack-rpc, you need to do the following steps:

  1. 从下载msgpack-RPC源的的git://github.com/msgpack/msgpack-rpc.git 的(特别是在Java的文件夹
  2. 更改主msgpack神器版本,你已经建立了一个。
  3. org.msgpack.rpc.loop.netty.NettyEventLoop 的,改变的 NioClientSocketChannelFactory 的到的 OioClientSocketChannelFactory(getWorkerExecutor())的。
  4. 构建MessagePack-RPC以同样的方式在主MessagePack JAR的情况下(见的步骤11 的上面)。
  1. Download the msgpack-rpc source from git://github.com/msgpack/msgpack-rpc.git (specifically, the "java" folder).
  2. Change the main msgpack artifact version to the one you've built.
  3. In org.msgpack.rpc.loop.netty.NettyEventLoop, change the NioClientSocketChannelFactory to OioClientSocketChannelFactory(getWorkerExecutor()).
  4. Build the MessagePack-RPC in the same way as in the case of the main MessagePack JAR (see Step 11 above).

NettyEventLoop 的更换是由于这个问题: <一个href="http://markmail.org/message/ypa3nrr64kzsyfsa">http://markmail.org/message/ypa3nrr64kzsyfsa

The NettyEventLoop replacement is due to this issue: http://markmail.org/message/ypa3nrr64kzsyfsa .

重要提示:我只测试同步的沟通。异步可能无法正常工作。

Important: I've only tested synchronous communication. Asynchronous might not work.

这是之前没有工作与Android的原因msgpack 0.6.7

的原因的错误是MessagePack使用未包括在Android SDK中几个java.beans中类。你可能使用的 MessagePackBeans 的注释。

The reason for the error is that MessagePack uses several java.beans classes that are not included in the Android SDK. You're probably using the MessagePackBeans annotation.

这是一个类似的问题来描述的的此处,为此,一般的解决方案概述的此处。不幸的是,在我们的情况下,它需要重建msgpack的。这是我做的事(你几乎可以肯定跳过步骤5和8,但我还没有尝试过这种方式):

This is a similar problem to the one described here, for which the general solution is outlined here. Unfortunately, in our case it requires a rebuild of msgpack. Here's what I did (you can almost certainly skip Steps 5 and 8, but I haven't tried it that way) :

  1. 从下载的 <一的MessagePack源href="https://github.com/msgpack/msgpack-java.git">https://github.com/msgpack/msgpack-java.git.
  2. 导入MessagePack源作为在IDE的项目。
  3. 下载Apache和谐源相关的程序包中的 <一个href="http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java">http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java
  4. 复制这些包到MessagePack项目的的src / main / java下的文件夹:
    • 的java.beans
    • 的java.beans.beancontext
    • org.apache.harmony.beans
    • org.apache.harmony.beans.internal.nls
  1. Download the MessagePack source from https://github.com/msgpack/msgpack-java.git.
  2. Import the MessagePack source as a project in your IDE.
  3. Download the Apache Harmony source for the relevant packages from http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java .
  4. Copy these packages into your MessagePack project's src/main/java folder:
    • java.beans
    • java.beans.beancontext
    • org.apache.harmony.beans
    • org.apache.harmony.beans.internal.nls
  • 的PropertyChangeListener
  • IndexedPropertyChangeEvent
  • 的PropertyChangeEvent
  • PropertyChangeListenerProxy
  • 的PropertyChangeSupport
  • 如果您正在使用Maven,在pom.xml的版本与更改为独特,运行Maven构建与安装的目标,然后添加依赖于你的Andr​​oid项目的版本
  • 如果你的不可以使用Maven,你必须运行的的目标蚂蚁与包括的的build.xml 的。更换msgpack JAR在你的这一个Android项目。
  • If you're using Maven, change the version in the pom.xml to something unique, run Maven build with the install goal, then add the dependency in your Android project with that version.
  • If you're not using Maven, you have to run the jar goal for Ant with the included build.xml. Replace the msgpack JAR in your Android project with this one.

这应该这样做。使用你的例子code,和我自己的数据类,我能够成功打包和解包的数据。

That should do it. Using your example code, and my own data class, I was successfully able to pack and unpack data.

整个更名仪式是由于对DEX编译器抱怨有关Java的事实。*包命名。

The entire renaming ritual is due to the fact that the DEX compiler complains about java.* package naming.

这篇关于使用MessagePack与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆