安卓上用org.eclipse.paho.client.mqttv3-1.0.2链接阿里云mqtt服务端报错Ivalid Client ID

taihuzhipan 2019-07-31 02:04:47
/******这里是客户端需要的参数*******/
public static String deviceName = "0862664D0169757";
public static String productKey = "a18laywNXCJ";
public static String secret = "************";

//用于测试的topic
private static String subTopic = "/" + productKey + "/" + deviceName + "/get";
private static String pubTopic = "/" + productKey + "/" + deviceName + "/update";

public static void StartConnect() throws Exception {
//客户端设备自己的一个标记,建议是MAC或SN,不能为空,32字符内
String clientId = "0862664D0169757";//InetAddress.getLocalHost().getHostAddress();

//设备认证
Map<String, String> params = new HashMap<String, String>();
params.put("productKey", productKey); //这个是对应用户在控制台注册的 设备productkey
params.put("deviceName", deviceName); //这个是对应用户在控制台注册的 设备name
params.put("clientId", clientId);
String t = System.currentTimeMillis() + "";
params.put("timestamp", t);

//MQTT服务器地址
String targetServer = "tcp://" + productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883";

//客户端ID格式,两个||之间的内容为设备端自定义的标记,字符范围[0-9][a-z][A-Z]
String mqttclientId = clientId + "|securemode=2,signmethod=hmacsha1,timestamp=" + t + "|";
String mqttUsername = deviceName + "&" + productKey; //mqtt用户名格式
String mqttPassword = "A9E93DE6CFF17C42C90DE890F8BD4DCBF99BC376";//SignUtil.sign(params, secret, "hmacsha1"); //签名

System.err.println("mqttclientId=" + mqttclientId+"&mqttPassword="+mqttPassword);

connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword, deviceName);
}

public static void connectMqtt(String url, String clientId, String mqttUsername,
String mqttPassword, final String deviceName) throws Exception {
MemoryPersistence persistence = new MemoryPersistence();
//SSLSocketFactory socketFactory = createSSLSocket();
final MqttClient sampleClient = new MqttClient(url, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setMqttVersion(4); // MQTT 3.1.1
//connOpts.setSocketFactory(socketFactory);

//设置是否自动重连
//connOpts.setAutomaticReconnect(true);

//如果是true,那么清理所有离线消息,即QoS1或者2的所有未接收内容
connOpts.setCleanSession(false);

connOpts.setUserName(mqttUsername);
connOpts.setPassword(mqttPassword.toCharArray());
connOpts.setKeepAliveInterval(65);

//LogUtil.print(clientId + "进行连接, 目的地: " + url);
sampleClient.connect(connOpts);

sampleClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
System.out.println("连接失败,原因:" + cause);
cause.printStackTrace();
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("接收到消息,来至Topic [" + topic + "] , 内容是:["
+ new String(message.getPayload(), "UTF-8") + "], ");
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//如果是QoS0的消息,token.resp是没有回复的
System.out.println("消息发送成功! " + ((token == null || token.getResponse() == null) ? "null"
: token.getResponse().getKey()));
}
});
...全文
569 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
taihuzhipan 2019-08-07
  • 打赏
  • 举报
回复
石沉大海了么?
taihuzhipan 2019-08-03
  • 打赏
  • 举报
回复
跪求大神帮助
weixin_40290083 2019-07-31
  • 打赏
  • 举报
回复
我在adroid上使用时是使用当前时间的毫秒值加上手机序列号做为clientId的,因为MQTT服务是自己建的服务器,测试环境就我自己在用,也没有出现什么问题,看资料上说是这个ID值不能相同,没有查到具体起到什么作用。
taihuzhipan 2019-07-31
  • 打赏
  • 举报
回复
引用 2 楼 weixin_40290083 的回复:
//客户端ID格式,两个||之间的内容为设备端自定义的标记,字符范围[0-9][a-z][A-Z],但是你的||之间有“,”,不知道是不是这个原因,我在使用MQTT时, clientId没有这么复杂,就是一个唯一标志符,
另外我在windows下面用C#这样链接是可以连接上后台的。不知道为啥到了安卓下面,同样的参数报这个错误
taihuzhipan 2019-07-31
  • 打赏
  • 举报
回复
引用 2 楼 weixin_40290083 的回复:
//客户端ID格式,两个||之间的内容为设备端自定义的标记,字符范围[0-9][a-z][A-Z],但是你的||之间有“,”,不知道是不是这个原因,我在使用MQTT时, clientId没有这么复杂,就是一个唯一标志符,
我改成纯字母也不行,去掉中间的逗号也不行,都是报这个异常,invalid client id。这个clientID是自己随便起的吗,还是要和后台有什么关联的。
weixin_40290083 2019-07-31
  • 打赏
  • 举报
回复
//客户端ID格式,两个||之间的内容为设备端自定义的标记,字符范围[0-9][a-z][A-Z],但是你的||之间有“,”,不知道是不是这个原因,我在使用MQTT时, clientId没有这么复杂,就是一个唯一标志符,
taihuzhipan 2019-07-31
  • 打赏
  • 举报
回复
sampleClient.connect(connOpts);调用这个函数的时候就报异常Invalid client ID (2)

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧