ZeroMQ 在 Android Java ( Android Studio )

ZeroMQ On Android Java ( Android Studio )

我正在尝试在我的 android 设备上编写 ZeroMQ 程序,我知道如何使用 ZeroMQ(来自 C++、Php、.Net,我一直在使用它。)如果我列出我到目前为止所做的事情:

try
{
    ZMQ.Context context = ZMQ.context(1);

    //  Socket to talk to server
    System.out.println("Connecting to hello world server...");

    ZMQ.Socket requester = context.socket(ZMQ.REQ);
    requester.connect("tcp://192.168.2.127:5555");

    for (int requestNbr = 0; requestNbr != 10; requestNbr++) {
        String request = "Hello";
        System.out.println("Sending Hello " + requestNbr);
        requester.send(request.getBytes(), 0);

        byte[] reply = requester.recv(0);
        System.out.println("Received " + new String(reply) + " " + requestNbr);
    }
    requester.close();
    context.term();


}
catch (Exception e)
{
   Log.e("Error", "Exception : " + e.getMessage());
}

当我运行这个时,我得到了如下异常:

01-03 02:20:55.200 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.200 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.200 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.210 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.220 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.230 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.230 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.240 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.250 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.260 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:20:55.270 12562-12562/mia.face.id.zeromq E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method mia.face.id.zeromq.MainActivity.access$super
01-03 02:21:08.080 12562-12562/mia.face.id.zeromq E/Error: Exception : null

其实我在编程方面并不是新手,而是 Android/Java 方面的新手。我不知道如何解决这个问题。我在互联网上搜索,但找不到任何东西。

如果你能帮助我,我会很高兴。

提前致谢。

(补充,我忘了包括这个:)

01-03 03:12:58.430 1374-1374/mia.face.id.zmqtestest E/Error: Exception
                                                         android.os.NetworkOnMainThreadException
                                                             at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
                                                             at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:168)
                                                             at libcore.io.IoBridge.sendto(IoBridge.java:490)
                                                             at java.nio.SocketChannelImpl.writeImpl(SocketChannelImpl.java:391)
                                                             at java.nio.SocketChannelImpl.write(SocketChannelImpl.java:349)
                                                             at java.nio.PipeImpl$PipeSinkChannel.write(PipeImpl.java:114)
                                                             at zmq.Signaler.send(Signaler.java:121)
                                                             at zmq.Mailbox.send(Mailbox.java:85)
                                                             at zmq.Ctx.sendCommand(Ctx.java:388)
                                                             at zmq.ZObject.sendCommand(ZObject.java:362)
                                                             at zmq.ZObject.sendPlug(ZObject.java:171)
                                                             at zmq.ZObject.sendPlug(ZObject.java:161)
                                                             at zmq.Own.launchChild(Own.java:126)
                                                             at zmq.SocketBase.addEndpoint(SocketBase.java:550)
                                                             at zmq.SocketBase.connect(SocketBase.java:542)
                                                             at org.zeromq.ZMQ$Socket.connect(ZMQ.java:1246)
                                                             at mia.face.id.zmqtestest.MainActivity.onClick(MainActivity.java:34)
                                                             at android.view.View.performClick(View.java:4438)
                                                             at android.view.View$PerformClick.run(View.java:18422)
                                                             at android.os.Handler.handleCallback(Handler.java:733)
                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                             at android.os.Looper.loop(Looper.java:136)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
                                                             at dalvik.system.NativeStart.main(Native Method)

从这里,我找到了答案,其实关于网络操作的方法论应该在后台任务中。

也许这会帮助其他人缩短他们用于研究的时间。

此致。