Android MQTT连接阿里云使用Json解析数据

这篇具有很好参考价值的文章主要介绍了Android MQTT连接阿里云使用Json解析数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

导入MQTT的JAR包

这里有两种方法:

1、在项目中添加依赖然后使用Studio 去下载库

2、直接下载JAR包,然后作为库进行导入

这里先介绍第一种方法:
切换到Android 目录:
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json按照如下图,添加依赖即可:
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json

  implementation("org.eclipse.paho.client.mqttv3-1.2.0")

android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json
第二种方法:
打开Android Studio 然后切换到Android 目录找到Lib文件夹,将MQTT的JAR包复制进去即可。
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json
然后点击project目录然后将MQTT JAR包导进去
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json
添加完成之后作为库导入工程中
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json

环境验证:

在MainActivity 任取一个地方输入MQTT 看是否有提示弹窗。
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json
到这里以及成功一半了。接下来就是设置一些权限问题

给程序进行联网权限

切换到按的Android 目录
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json
找到如下的目录:
android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json
添加如代码:

 <uses-permission android:name="android.permission.INTERNET" />
    <!--允许程序获取网络状态-->
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:background="#51CCDC"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:background="#673AB7"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:text="桂航果蔬种植基地"
                android:textSize="25sp"
                android:textColor="#060A0C"
                android:layout_height="match_parent">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:layout_marginTop="20dp">
            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                app:cardCornerRadius="20dp">
                <ImageView
                    android:id="@+id/m_img2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg3"
                    />
            </androidx.cardview.widget.CardView>
        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#51CCDC"
            android:orientation="vertical">
            <LinearLayout
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="match_parent"
                android:background="@drawable/shape_corner"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="5dp"
                android:padding="30dp">
                <!--左文字-->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="学号:2020070230226"
                    android:textColor="@color/black"
                    android:textSize="16sp">
                </TextView>
                <!--左文字-->
                <TextView
                    android:id="@+id/m_mqtt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text= "姓名:xiao!"
                    android:textColor="@color/black"
                    android:textSize="16sp">
                </TextView>
            </LinearLayout>
        </LinearLayout>
        <!-- vertical 竖直排列 -->
        <!-- horizontal 竖直排列 -->
        <!-- 数据解析界面 -->
        <LinearLayout
            android:layout_marginTop="10dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFF">
            <!-- 包裹其他界面-->
            <LinearLayout
                android:layout_margin="5px"
                android:padding="3dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:layout_height="match_parent">
                    <!--气温-->
                    <LinearLayout
                        android:layout_width="160dp"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:background="@drawable/shape_corner"
                        android:layout_height="105dp">
                        <ImageView
                            android:id="@+id/test"
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:layout_marginTop="10dp"
                            android:src="@drawable/air_temp"
                            android:layout_marginRight="15dp">
                        </ImageView>
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:orientation="vertical"
                            android:layout_height="wrap_content">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="5dp"
                                android:text="温度"
                                android:textSize="25sp">
                            </TextView>
                            <!-- 数值 -->
                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                                <TextView
                                    android:id="@+id/air_temp"
                                    android:layout_width="wrap_content"
                                    android:text=" 0 "
                                    android:textSize="25sp"
                                    android:layout_height="wrap_content">
                                </TextView>
                            </LinearLayout>
                        </LinearLayout>
                    </LinearLayout>
                    <!--The End -->
                    <!-- 湿度-->
                    <LinearLayout
                        android:layout_marginLeft="25dp"
                        android:layout_width="160dp"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:background="@drawable/shape_corner"
                        android:layout_height="105dp">
                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:layout_marginTop="10dp"
                            android:src="@drawable/air_humi"
                            android:layout_marginRight="15dp">
                        </ImageView>
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:orientation="vertical"
                            android:layout_height="wrap_content">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="5dp"
                                android:text="湿度"
                                android:textSize="20sp">
                            </TextView>
                            <!-- 数值 -->
                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                                <TextView
                                    android:id="@+id/air_humi"
                                    android:layout_width="wrap_content"
                                    android:text=" 0 "
                                    android:textSize="25sp"
                                    android:layout_height="wrap_content">
                                </TextView>
                            </LinearLayout>
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>
                <!--The End -->

            </LinearLayout>
            <!-- 2 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:background="#FFFF"
                android:layout_margin="5px"
                android:padding="3dp"
                android:layout_height="wrap_content">
                <!-- Layout1-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:layout_height="match_parent">
                    <!--气温-->
                    <!--Left1 -->
                    <LinearLayout
                        android:layout_width="160dp"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:background="@drawable/shape_corner"
                        android:layout_height="105dp">
                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:layout_marginTop="10dp"
                            android:src="@drawable/soil_humi"
                            android:layout_marginRight="15dp">
                        </ImageView>
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:orientation="vertical"
                            android:layout_height="wrap_content">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="5dp"
                                android:text="土壤湿度"
                                android:textSize="20sp">
                            </TextView>
                            <!-- 数值 -->
                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                                <TextView
                                    android:id="@+id/sloi_humi"
                                    android:layout_width="wrap_content"
                                    android:text=" 0 "
                                    android:textSize="25sp"
                                    android:layout_height="wrap_content">
                                </TextView>
                            </LinearLayout>

                        </LinearLayout>
                    </LinearLayout>
                    <!--The End -->

                    <!-- 湿度-->
                    <LinearLayout
                        android:layout_width="160dp"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="25dp"
                        android:background="@drawable/shape_corner"
                        android:layout_height="105dp">
                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:layout_marginTop="10dp"
                            android:src="@drawable/n2"
                            android:layout_marginRight="15dp">
                        </ImageView>
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:orientation="vertical"
                            android:layout_height="wrap_content">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="5dp"
                                android:text="土壤含氮"
                                android:textSize="20sp">
                            </TextView>
                            <!-- 数值 -->
                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                                <TextView
                                    android:id="@+id/soli_n"
                                    android:layout_width="wrap_content"
                                    android:text=" 0 "
                                    android:textSize="25sp"
                                    android:layout_height="wrap_content">
                                </TextView>
                            </LinearLayout>
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
            <!-- The End -->
            <!-- 3 -->
            <LinearLayout
                android:layout_margin="5px"
                android:padding="3dp"
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:layout_width="160dp"
                    android:background="@drawable/shape_corner"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_height="105dp">
                    <ImageView
                        android:layout_weight="1"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/p2"
                        android:layout_marginRight="15dp">
                    </ImageView>
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:orientation="vertical"
                        android:layout_height="wrap_content">
                        <TextView
                            android:layout_marginTop="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_width="wrap_content"
                            android:text="土壤含磷"
                            android:textSize="20sp"
                            android:layout_height="wrap_content">
                        </TextView>
                        <TextView
                            android:id="@+id/soil_p"
                            android:layout_marginTop="5dp"
                            android:layout_width="wrap_content"
                            android:text=" 0 "
                            android:textSize="25sp"
                            android:layout_height="wrap_content">
                        </TextView>
                    </LinearLayout>
                </LinearLayout>
                <!--L1-->
                <LinearLayout
                    android:layout_marginLeft="25dp"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:layout_width="160dp"
                    android:orientation="horizontal"
                    android:background="@drawable/shape_corner"
                    android:layout_height="105dp">
                    <ImageView
                        android:layout_weight="1"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/k2"
                        android:layout_marginRight="15dp">
                    </ImageView>
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:orientation="vertical"
                        android:layout_height="wrap_content">
                        <TextView
                            android:layout_marginRight="5dp"
                            android:layout_width="wrap_content"
                            android:text="土壤含钾"
                            android:textSize="20sp"
                            android:layout_height="wrap_content">
                        </TextView>
                        <TextView
                            android:id="@+id/soil_k"
                            android:layout_width="wrap_content"
                            android:text=" 0 "
                            android:textSize="25sp"
                            android:layout_height="wrap_content">
                        </TextView>
                    </LinearLayout>
                </LinearLayout>
                <!--Layout2-->
            </LinearLayout>
            <!-- The End -->
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

效果如下:

android studio通过mqtt连接阿里云物联网平台,Android IOT 上位机,android,阿里云,json文章来源地址https://www.toymoban.com/news/detail-768593.html

MainActitive.java 主要代码如下:

package com.example.ks_wfs;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.net.wifi.aware.DiscoverySession;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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.concurrent.ScheduledExecutorService;
public class MainActivity extends AppCompatActivity {
    private Handler handler;
    private String serverUri = "自己的IP地址";
/*    private String serverUri = "tcp://iot.qaeb.cn:1883";*/
    /*
          这里可以填上各种云平台的物联网云平的域名+1883端口号,什么阿里云腾讯云百度云天工物接入都可以,
          这里我填的是我在我的阿里云服务器上搭建的EMQ平台的地址,
          注意:前缀“tcp://”不可少,之前我没写,怎么都连不上,折腾了好久
    */
    private String userName = " ";
    private String passWord = " ";
    private String clientId = "app"+System.currentTimeMillis(); //clientId很重要,不能重复,否则就会连不上,所以我定义成 app+当前时间
    private String mqtt_sub_topic = "/iotsoil/post";          //需要订阅的主题
    private String mqtt_pub_topic = "xiao";                  //需要发布的主题
    private MqttClient mqtt_client;                         //创建一个mqtt_client对象
    MqttConnectOptions options;
    private  TextView air_temp;
    private  TextView air_humi;
    private  TextView sloi_humi;
    private  TextView soli_n;
    private  TextView soil_p;
    private  TextView soil_k;
    private ImageView test;
    private ScheduledExecutorService scheduler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * xiaobai
         * 2023.10.10
         * Version V1.0
         */
        /*绑定UI文件*/
        UI_Init();/*类似单片机的初始化*/
        mqtt_init_Connect();/*初始化连接函数*/
        /*回调函数,数据会在回调函数里面*/
        mqtt_client.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable throwable) {
            }
            @Override
            public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                /*数据到这了*/
                final String msg = new String(mqttMessage.getPayload());
                Log.d("MQTTRCV", msg);//日志信息,可以查看
                System.out.println(msg);/*打印得到的数据*/
                /*更新UI线程*/
 /*               runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                            *//*开始解析数据*//*
                           *//*数据解析部分这里使用的是Json解析*//*
                           *//*为了避免闪退,使用try来处理异常情况*//*
                        try {
                            JSONObject jsonObject = new JSONObject(String.valueOf(msg));
                            String method = jsonObject.getString("method");
                            JSONObject params = jsonObject.getJSONObject("params");
                            int soilHumidity = params.getInt("soilHumidity");
                            int currentTemperature = params.getInt("CurrentTemperature");
                            int currentHumidity = params.getInt("CurrentHumidity");
                            int n = params.getInt("N");
                            int p = params.getInt("P");
                            int k = params.getInt("K");
                                    *//*更新数据到UI*//*
                            air_temp.setText(String.valueOf(currentTemperature));
                            air_humi.setText(String.valueOf(currentHumidity));
                            sloi_humi.setText(String.valueOf(soilHumidity));
                            soli_n.setText(String.valueOf(n));
                            soil_p.setText(String.valueOf(p));
                            soil_k.setText(String.valueOf(k));
                                    *//*打印日志信息*//*
                            Log.d("TAG", "method: " + method);
                            Log.d("TAG", "soilHumidity: " + soilHumidity);
                            Log.d("TAG", "currentTemperature: " + currentTemperature);
                            Log.d("TAG", "currentHumidity: " + currentHumidity);
                            Log.d("TAG", "N: " + n);
                            Log.d("TAG", "P: " + p);
                            Log.d("TAG", "K: " + k);

                        }catch (JSONException e)
                        {
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this ,"数据解析异常!",Toast.LENGTH_LONG).show();
                        }
                    }
                });*/
                /*下面是带弹窗显示的代码数据已经解析完成,解开注释即可*/

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("MQTT消息");
                        builder.setMessage(msg);
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // 点击确定按钮后的操作
                             /*   开始解析数据*/
                            /*    数据解析部分这里使用的是Json解析
                                为了避免闪退,使用try来处理异常情况*/
                                try {
                                    JSONObject jsonObject = new JSONObject(String.valueOf(msg));
                                    String method = jsonObject.getString("method");
                                    JSONObject params = jsonObject.getJSONObject("params");
                                    int soilHumidity = params.getInt("soilHumidity");
                                    int currentTemperature = params.getInt("CurrentTemperature");
                                    int currentHumidity = params.getInt("CurrentHumidity");
                                    int n = params.getInt("N");
                                    int p = params.getInt("P");
                                    int k = params.getInt("K");
                               /*     更新UI*/
                                    air_temp.setText(String.valueOf(currentTemperature));
                                    air_humi.setText(String.valueOf(currentHumidity));
                                    sloi_humi.setText(String.valueOf(soilHumidity));
                                    soli_n.setText(String.valueOf(n));
                                    soil_p.setText(String.valueOf(p));
                                    soil_k.setText(String.valueOf(k));
                               /*     打印输出的信息*/
                                    Log.d("TAG", "method: " + method);
                                    Log.d("TAG", "soilHumidity: " + soilHumidity);
                                    Log.d("TAG", "currentTemperature: " + currentTemperature);
                                    Log.d("TAG", "currentHumidity: " + currentHumidity);
                                    Log.d("TAG", "N: " + n);
                                    Log.d("TAG", "P: " + p);
                                    Log.d("TAG", "K: " + k);

                                }catch (JSONException e)
                                {
                                    e.printStackTrace();
                                    Toast.makeText(MainActivity.this ,"数据解析异常!",Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                        builder.show();
                    }
                });
            }
            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            }
        });
        /*点击事件*/
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PublishMessage(mqtt_pub_topic,"{\"params\":{\"我是小白,你好呀\":999 } }");
            }
        });

    }
    public void mqtt_init_Connect()
    {
        try {
            //实例化mqtt_client,填入我们定义的serverUri和clientId,然后MemoryPersistence设置clientid的保存形式,默认为以内存保存
            mqtt_client = new MqttClient(serverUri,clientId,new MemoryPersistence());
            //创建并实例化一个MQTT的连接参数对象
            options = new MqttConnectOptions();
            //然后设置对应的参数
            options.setUserName(userName);                  //设置连接的用户名
            options.setPassword(passWord.toCharArray());    //设置连接的密码
            options.setConnectionTimeout(30);               // 设置超时时间,单位为秒
            options.setKeepAliveInterval(50);               //设置心跳,30s
            options.setAutomaticReconnect(true);            //是否重连
            //设置是否清空session,设置为false表示服务器会保留客户端的连接记录,设置为true表示每次连接到服务器都以新的身份连接
            options.setCleanSession(false);
            /*初始化成功之后就开始连接*/
            connect();
            Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_LONG).show();
        }catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_LONG).show();
        }
    }
    public void connect(){
        //连接mqtt服务器
        try {
            mqtt_client.connect(options);
            mqtt_client.subscribe(mqtt_sub_topic);
            Toast.makeText(this ,"开始建立连接.....!",Toast.LENGTH_LONG).show();
        }catch (Exception e) {
            e.printStackTrace();
            Log.d("MQTTCon","mqtt连接失败");
            Toast.makeText(this ,"mqtt连接失败!",Toast.LENGTH_LONG).show();
        }
    }
    /*绑定各个UI控件*/
    public void UI_Init()
    {
        air_temp  =  findViewById(R.id.air_temp); //空气温度
        air_humi  =  findViewById(R.id.air_humi);//空气湿度
        sloi_humi =  findViewById(R.id.sloi_humi);//土壤湿度
        soli_n    =  findViewById(R.id.soli_n);//土壤含N量
        soil_p    =  findViewById(R.id.soil_p);//土壤含P量
        soil_k    =  findViewById(R.id.soil_k);//土壤含K量
        test      =  findViewById(R.id.test);//测试按钮
    }
    /*发布函数!*/
    private void PublishMessage(String topic,String Message2)
    {
        if(mqtt_client ==null || !mqtt_client.isConnected())
        {
            return;
        }
        MqttMessage message = new MqttMessage();
        message.setPayload(Message2.getBytes());
        try{
            mqtt_client.publish(topic,message);

        }catch (MqttException e)
        {
            e.printStackTrace();
        }
    }

}

End

到了这里,关于Android MQTT连接阿里云使用Json解析数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android:Json-Gson数据解析

     JSONObject jsonObject = new JSONObject();             jsonObject.put(\\\"name\\\",\\\"admin\\\");             jsonObject.put(\\\"passWord\\\",\\\"123456\\\");             //jsonObject.getString(\\\"name\\\")            // textView.setText(jsonObject.toString());             //谷歌:api             JsonObject jsonObject1 = new JsonObject();      

    2024年02月17日
    浏览(39)
  • android studio 使用MQTT协议连接onenet 注意事项和相关函数

    一、注意事项: 1.添加依赖: 2.在注册表允许打开网络 3.在application     /application之间添加用于支持 MQTT(Message Queuing Telemetry Transport)通信协议的 Eclipse Paho Android 服务。 4.打开项目中的 gradle.properties 文件,并确保以下属性设置为true------更新所有的支持库到AndroidX中。 二、

    2024年04月26日
    浏览(38)
  • 1-基本控制篇(阿里云物联网平台)-C#,网页,android,微信小程序,单片机等使用MQTT接入阿里云物联网平台

    \\\"ProductKey\\\": \\\"a1m7er1nJbQ\\\", \\\"DeviceName\\\": \\\"Mqtt\\\", \\\"DeviceSecret\\\": \\\"7GUrQwgDUcXWV3EIuLwdEvmRPWcl7VsU\\\" 如何使用MQTT协议模拟设备快速接入物联网平台_物联网平台(IoT)-阿里云帮助中心    阿里云的说明文档 a1m7er1nJbQ.iot-as-mqtt.${region}.aliyuncs.com a1m7er1nJbQ.iot-as-mqtt.cn-shanghai.aliyuncs.com 112233445566|securemode=3,s

    2024年04月12日
    浏览(121)
  • Android+MQTT(Android APP 连接至MQTT服务器)

    这段时间因为毕设的需求,接触学习了一下用Android app连接MQTT服务器,然后对下位机(STM32/ESP8266)进行数据通讯的一个小设计,本篇文章介绍app+MQTT服务器这一段。 实现原理: 这里设计的是一个监测控制智能风扇的APP,可以实时监测当前温度、湿度及下位机的传感器、控制

    2024年02月02日
    浏览(42)
  • STM32、ESP8266与MQTT连接阿里云物联网的串口通信异常解析

    STM32、ESP8266与MQTT协议连接阿里云物联网平台时常见的串口通信异常介绍 在构建物联网应用时,STM32、ESP8266与MQTT协议的结合是实现设备与网络间稳定通信的关键。然而,在连接阿里云物联网平台的过程中,串口通信异常成为了一个常见的挑战。本文将探讨这些异常现象及其可

    2024年04月22日
    浏览(32)
  • Unity文本框解析读取mqtt服务器JSON数据

    本次内容是讲述如何将mqtt服务器中接收到的数据在Unity3D的文本框控件中显示JSON键值对中的“值”。 需求: 1.GameObject——UI——Text (将Unity 3D的文本框控件置于场景) 命名空间引用: using UnityEngine.UI; 2. Newtonsoft插件 命名空间引用: Newtonsoft.Json.Linq; 3.MQTT通讯需求: (1)h

    2023年04月08日
    浏览(70)
  • Android json功能解析

      JAVAScript Object Notation是一种轻量级的数据交换格式具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持)。  JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。JSO

    2024年01月20日
    浏览(27)
  • MQTT连接阿里云物联网上报物模型数据

    目录 1. 创建产品(物联网平台 - 产品 - 创建产品) 2. 为产品添加设备 3. 添加物模型 4. mqtt.fx连接测试 5. 调试物模型 6. 使用mqtt.fx上报温度数据  我这里再新建一个名为 \\\"Test01\\\" 的产品作为演示 选择 \\\"添加自定义功能\\\" 添加 温度 属性: 创建好后,点击 发布上线 MQTT.fx是一款基

    2024年04月12日
    浏览(36)
  • 使用mqtt连接阿里云物联网平台代码,云流转

    安装express .命令 npm install express 安装alibabacloud-iot-device-sdk 命令 npm install alibabacloud-iot-device-sdk. 使用云流转,实现两个设备的互通(云流转操作看之前的博客) 代码:

    2024年02月02日
    浏览(44)
  • Android网络编程,HTTP请求和Json解析

    以下代码模拟了点击按钮请求百度的网页源码: 其中需要注意的是Android在API27之后不再支持明文访问HTTP,需要在manifest文件中配置属性允许使用明文访问, 并且Url需要使用https layout.xml 字节流转换字符串工具类: 主类.java: 配置manifest.xml文件: 将上述代码中的webview相关内容

    2023年04月09日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包