- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在为 android 构建一个简单的 MQTT 客户端,我在 RMBS 控制台上收到“客户端标识符的套接字错误”错误。这只发生在客户端的 android 实现中(我还创建了一个 Java 桌面客户端并且运行没有问题)。对于 Android 客户端,我起诉了 Paho Java 客户端库。这是我的代码:
这是 Android 客户端:
package com.example.mqttdroid;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MQTTClient extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mqttclient);
// new BackTask().execute(); not used because (seems to be the problem)
MqttConnectOptions conOpts = new MqttConnectOptions();
conOpts.setKeepAliveInterval(30);
conOpts.setWill(client.getTopic("Error"), "something bad happened".getBytes(), 1, true);
client.connect(conOpts);
client.subscribe("/House/Kitchen/Bulb");
client.setCallback( new MqttCallback() {
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String arg0, MqttMessage arg1)
throws Exception {
// TODO Auto-generated method stub
Toast.makeText(Main.this, arg0.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mqttclient, menu);
return true;
}
/*public class BackTask extends AsyncTask<Void, Void, Void>{
private MqttClient client;
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
client = new MqttClient("tcp://"Ip of machine running RSMB":1883", "ANDROID1", new MemoryPersistence());
client.connect();
client.subscribe("House/Kitchen/Bulb");
} catch (MqttException e) {
// TODO Auto-generated catch block
Log.e("ERROR", "NOT CONNECTED");
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
client.setCallback( new MqttCallback() {
@Override
public void messageArrived(MqttTopic arg0, MqttMessage arg1)
throws Exception {
// TODO Auto-generated method stub
Toast.makeText(MQTTClient.this, arg0.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void deliveryComplete(MqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
});
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}*/
这是桌面 Java 客户端:
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
public class MQTTBaseClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MqttClientPersistence persistence;
try {
MqttClient client = new MqttClient("tcp://localhost:1883", "PC",new MemoryPersistence());
MqttConnectOptions conOpts = new MqttConnectOptions();
conOpts.setKeepAliveInterval(30);
conOpts.setWill(client.getTopic("Error"), "something bad happened".getBytes(), 1, true);
client.connect(conOpts);
MqttMessage msg = new MqttMessage("hello".getBytes());
msg.setQos(0);
msg.setRetained(true);
MqttTopic topic = client.getTopic("House/Kitchen/Bulb");
client.subscribe("House/Kitchen/Bulb");
try {
client.setCallback( new MqttCallback() {
@Override
public void messageArrived(MqttTopic arg0, MqttMessage arg1)
throws Exception {
// TODO Auto-generated method stub
System.out.println(arg1.toString());
}
@Override
public void deliveryComplete(MqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
});
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
topic.publish(msg);
} catch (MqttPersistenceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
一些注意事项:
我通过 WiFi 连接到我的 Android 设备,当我运行 Java Dekstop 客户端时,我的桌面也是如此。
Java Destop 客户端与 RSMB 在同一台机器上运行
Java 桌面客户端创建并订阅主题“House/Kitchen/Bulb”,并发送一条消息,字符串为“Hello”
Android 客户端还订阅“House/Kitchen/Bulb”并尝试显示包含收到消息的 Toast。
我已经在android manifest上添加了上网权限
Android 设备似乎可以很好地连接到代理,但是只要我初始化 Java 桌面服务客户端(或 Eclipse 中的 Paho 客户端插件并发布消息),我就会收到提到的错误。
我在运行 RSMB 的同一台机器上使用模拟器运行应用程序,但我遇到了同样的错误。
可能是什么问题?
更新:
最初,我遇到了“主线程上的网络”异常,所以我将连接操作移到了 AsyncTask 中。现在,当我使用 Java 客户端发布消息时,Android 客户端似乎仍然处于连接状态(Asynctask 可能一直在创建问题),但是 MqttCallback() 的 messageArrived() 似乎没有被调用。
更新 2:
我设法让它发挥作用。这是我现在使用的代码:
package com.example.mqttphone;
*import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;*
public class Main extends Activity {
protected static String msg;
public MqttClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
client = new MqttClient("tcp://10.1.201.27:1883", "ANDROID1", new MemoryPersistence());
MqttConnectOptions conOpts = new MqttConnectOptions();
conOpts.setKeepAliveInterval(30);
conOpts.setWill(client.getTopic("Error"), "something bad happened".getBytes(), 1, true);
client.setCallback( new MqttCallback() {
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String arg0, MqttMessage arg1)
throws Exception {
// TODO Auto-generated method stub
Main.msg = arg1.toString();
Main.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(Main.this, msg, Toast.LENGTH_LONG).show();
}
});
Log.e("MESSAGE RECEIVED", arg1.toString());
}
});
client.connect(conOpts);
//MqttMessage msg = new MqttMessage("ANDROID MESSAGE".getBytes());
//client.getTopic("world").publish(msg);
if(client.isConnected()){
client.subscribe("/House/Kitchen/Bulb");
Toast.makeText(this, "CONNECTED", Toast.LENGTH_SHORT).show();
}
} catch (MqttException e) {
// TODO Auto-generated catch block
Log.e("ERROR", "NOT CONNECTED");
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
最佳答案
你好
我已经使用了你的代码,但我无法将我的 paho 客户端连接到我的 apollo 服务器,我遇到了以下错误。
I/global(677): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
E/ERROR(677): NOT CONNECTED
W/System.err(677): (32109) - java.io.EOFException
W/System.err(677): at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:127)
W/System.err(677): at java.lang.Thread.run(Thread.java:1096)
W/System.err(677): Caused by: java.io.EOFException
W/System.err(677): at java.io.DataInputStream.readFully(DataInputStream.java:266)
W/System.err(677): at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:63)
W/System.err(677): at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:94)
W/System.err(677): ... 1 more
谢谢
吉里什
关于android - 使用 android 时 RSMB MQTT 代理上客户端标识符的套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064811/
现在我正在做一个关于 Android 推送的 RnD,首先我在做:使用 RSMB 的 MQTT 问题是它能处理多少客户?我只听到 1032,在那种情况下这对我的应用程序不利。 (如果它是可配置的怎么办
我正在为 android 构建一个简单的 MQTT 客户端,我在 RMBS 控制台上收到“客户端标识符的套接字错误”错误。这只发生在客户端的 android 实现中(我还创建了一个 Java 桌面客户
我目前使用 RSMB 作为移动设备的 MQTT 连接。当消息通过时,将发布的消息存储到 SQL 数据库中的最佳方法是什么? 现在我必须在发布到主题之前 POST 到访问数据库的 PHP 页面。 谢谢
我的问题是关于通过 MQTT-SN 网关使用 MQTT 主题名称和 MQTT-SN 主题 ID 配置 RSMB。 使用“Getting started with the Really Small Me
我是一名优秀的程序员,十分优秀!