27,307
社区成员




您好本人在校大学生想用Android studio连接到阿里云上并获取数据以下代码没报错运行在app上是显示连接错误,麻烦您可以帮我看一下吗谢谢:
package com.example.smarthome;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
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 org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
//mqtt
public class HomeActivity extends AppCompatActivity {
private MqttClient client;
private MqttConnectOptions options;
private Handler handler;
private ScheduledExecutorService scheduler;
private String productKey="k08o5pYygtP";
private String deviceName="mqtt_stm32";
private String deviceSecret="c698a4a26bbf16231e2134a3affd1342";
//MQTT发布的主题
private final String pub_topic= "/sys/k08o5pYygtP/app_dev/thing/event/property/post";
//MQTT订阅的主题
private final String sub_topic = "/sys/k08o5pYygtP/app_dev/thing/service/property/set";
private int temperature=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//TextView
TextView tv_temp=findViewById(R.id.TV_temp);
//home
BottomNavigationView bottomNavigationView=(findViewById(R.id.button_navigation));
bottomNavigationView.setSelectedItemId(R.id.home);
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuitem) {
if (menuitem.getItemId()==R.id.about)
{
startActivity(new Intent(getApplicationContext(), AboutActivity2.class));
overridePendingTransition(0,0);
return true;
}
if(menuitem.getItemId()==R.id.home)
{
return true;
}
return false;
}
});
mqtt_init();
start_reconnect();
//MQTT数据订阅
handler = new Handler() {
@SuppressLint("SetTextI18n")
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1: //开机校验更新回传
break;
case 2: // 反馈回传
break;
case 3: //MQTT 收到消息回传 UTF8Buffer msg=new UTF8Buffer(object.toString());
String message = msg.obj.toString();
Log.d("nicecode", "handleMessage: "+ message);
try {
JSONObject jsonObjectALL = null;
jsonObjectALL = new JSONObject(message);
JSONObject items = jsonObjectALL.getJSONObject("items");
JSONObject obj_temp = items.getJSONObject("temp");
// JSONObject obj_humi = items.getJSONObject("humi");
temperature = obj_temp.getInt("value");
//humidity = obj_humi.getInt("value");
tv_temp.setText(temperature + "");
// tv_humi.setText(humidity + "");
Log.d("nicecode", "temp: "+ temperature);
// Log.d("nicecode", "humi: "+ humidity);
} catch (JSONException e) {
e.printStackTrace();
break;
}
break;
case 30: //连接失败
Toast.makeText(HomeActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
break;
case 31: //连接成功
Toast.makeText(HomeActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
try {
client.subscribe(sub_topic, 1);
} catch (MqttException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
};
}
//MQTT初始化
private void mqtt_init() {
try {
String clientId = "k08o5pYygtP.mqtt_stm32";
Map<String, String> params = new HashMap<String, String>(16);
params.put("productKey", productKey);
params.put("deviceName", deviceName);
params.put("clientId", clientId);
String timestamp = String.valueOf(System.currentTimeMillis());
params.put("timestamp", timestamp);
// cn-shanghai
String host_url ="tcp://"+ productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883";
String client_id = clientId + "|securemode=2,signmethod=hmacsha256,timestamp=" +timestamp+ "|";
String user_name = deviceName + "&" + productKey;
String password = com.example.smarthome.AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1");
//host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
System.out.println(">>>" + host_url);
System.out.println(">>>" + client_id);
//connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword);
client = new MqttClient(host_url, client_id, new MemoryPersistence());
//MQTT的连接设置
options = new MqttConnectOptions();
//设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(false);
//设置连接的用户名
options.setUserName(user_name);
//设置连接的密码
options.setPassword(password.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(60);
//设置回调
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
//连接丢失后,一般在这里面进行重连
System.out.println("connectionLost----------");
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//publish后会执行到这里
System.out.println("deliveryComplete---------" + token.isComplete());
}
@Override
public void messageArrived(String topicName, MqttMessage message)
throws Exception {
//subscribe后得到的消息会执行到这里面
System.out.println("messageArrived----------");
Message msg = new Message();
//封装message包
msg.what = 3; //收到消息标志位
msg.obj =message.toString();
//发送messge到handler
handler.sendMessage(msg); // hander 回传
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void mqtt_connect() {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (!(client.isConnected())) //如果还未连接
{
client.connect(options);
Message msg = new Message();
msg.what = 31;
// 没有用到obj字段
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = 30;
// 没有用到obj字段
handler.sendMessage(msg);
}
}
}).start();
}
//MQTT的连接
private void start_reconnect() {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (!client.isConnected()) {
mqtt_connect();
}
}
}, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS);
}
}