我刚开始使用Message Queues编程,我一直在尝试使用ZeroMQ
的简单Android应用程序。我在单独的线程中在同一设备上运行服务器和客户端程序。这是我的Server
课程:
public class ZeroMQServer implements Runnable{
private Handler uiThreadHandler;
public ZeroMQServer(Handler uiThreadHandler) {
this.uiThreadHandler = uiThreadHandler;
}
@Override
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REP);
socket.bind("tcp://*:5570");
while(!Thread.currentThread().isInterrupted()) {
byte[] msg = socket.recv(0);
uiThreadHandler.sendMessage(
Util.bundledMessage(uiThreadHandler, new String(msg)));
socket.send(Util.reverseInPlace(msg)+"", 0);
}
socket.close();
context.term();
}
这是客户端类:
public class ZeroMQMessageTask extends AsyncTask<String, Void, String> {
private final Handler uiThreadHandler;
public ZeroMQMessageTask(Handler uiThreadHandler){
this.uiThreadHandler = uiThreadHandler;
}
@Override
protected String doInBackground(String... params) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REQ);
socket.connect("tcp://localhost:5570");
socket.send(params[0].getBytes(), 0);
String result = new String(socket.recv(0));
socket.close();
context.term();
return result;
}
@Override
protected void onPostExecute(String result) {
uiThreadHandler.sendMessage(Util.bundledMessage(uiThreadHandler, result));
}
这是主要活动的片段,它在另一个Thread中启动服务器并实现clickListener来调用客户端。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.text_console);
editText = (EditText)findViewById(R.id.text_message);
new Thread(new ZeroMQServer(serverMessageHandler)).run();
findViewById(R.id.button_send_message).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
new ZeroMQMessageTask(clientMessageHandler).execute(getTaskInput());
}
protected String getTaskInput() {
return editText.getText().toString();
}
});
}
我遇到的问题是,一旦应用程序启动它就会自动关闭。当我从服务器中删除socket.bind("tcp://*:5570");
行时,它不会再关闭,但也不会按照预期的方式工作,因为服务器不会监听。
我在清单中授予INTERNET
和ACCESS_WIFI_STATE
权限。
对此事的任何帮助表示赞赏。
编辑:
我在 .bind()
行添加了一个断点,这是我得到的错误:
Didn't find class "java.net.StandardProtocolFamily" on path:
DexPathList[
[zip file "/data/app/com.example.victor.zeromqrabbitmq-1/base.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_dependencies_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_0_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_1_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_2_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_3_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_4_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_5_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_6_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_7_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_8_apk.apk",
zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_9_apk.apk"
],
nativeLibraryDirectories=[
/data/app/com.example.victor.zeromqrabbitmq-1/lib/arm,
/vendor/lib, /system/lib
]
]
EDIT2:完整的logcat错误:
09-01 00:44:19.878 13082-13205/com.example.victor.zeromqrabbitmq E/Error: Exception:
09-01 00:44:20.188 13082-13205/com.example.victor.zeromqrabbitmq E/AndroidRuntime: FATAL EXCEPTION:
AsyncTask #1
Process: com.example.victor.zeromqrabbitmq, PID: 13082
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/net/StandardProtocolFamily;
at zmq.io.net.tcp.TcpAddress.family(TcpAddress.java:50)
at zmq.io.net.tcp.TcpListener.setAddress(TcpListener.java:171)
at zmq.SocketBase.bind(SocketBase.java:363)
at org.zeromq.ZMQ$Socket.bind(ZMQ.java:2333)
at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:24)
at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:10)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.net.StandardProtocolFamily" on path: DexPathList[[zip file "/data/app/com.example.victor.zeromqrabbitmq-1/base.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.victor.zeromqrabbitmq-1/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at zmq.io.net.tcp.TcpAddress.family(TcpAddress.java:50)
at zmq.io.net.tcp.TcpListener.setAddress(TcpListener.java:171)
at zmq.SocketBase.bind(SocketBase.java:363)
at org.zeromq.ZMQ$Socket.bind(ZMQ.java:2333)
at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:24)
at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:10)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Suppressed: java.lang.ClassNotFoundException: java.net.StandardProtocolFamily
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
09-01 00:44:20.699 13082-13206/com.example.victor.zeromqrabbitmq E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb810d418
看起来像一个简单的classNotFound错误,但它引用了一个带有“android包”的类,这意味着它不属于任何外部库。那怎么可能找不到呢?
答案 0 :(得分:0)
可能删除*并将其替换为localhost
答案 1 :(得分:0)
规则0)习惯于始终使用 Poller.poll( aTimeoutInMILLISECONDs )
方法调用,以便始终控制需要提供的事件(如果有的话)在外部事件循环生态系统中
规则1)切勿在严肃的生产级代码because of it's inherent hard-wired affinity to fall into an un-salvageable mutual dead-lock.
中使用REQ/REP
规则2)始终更喜欢明确的地址 .bind()
-s。隐式和通配符扩展不是策划生产级部署的一种方式,因为您的系统将依赖于外部翻译 - 而非 .bind()
到显式的全球广告地址(即使教科书示例显示其他实践......)
规则3)不要犹豫使用assert();
或try: / except:
处理练习,这样您就可以很好地本地化并诊断任何异常的根本原因。大多数语言绑定都提供ZeroMQ错误工具。
答案 2 :(得分:0)
您遇到的问题是您使用的JeroMQ版本与Android不兼容。此拉取请求:https://github.com/zeromq/jeromq/pull/471添加了对Android 10及更高版本的支持,但仅适用于0.4.3-SNAPSHOT。