个人响应式网站建设app推广平台网站

当前位置: 首页 > news >正文

个人响应式网站建设,app推广平台网站,小游戏网页版链接,网站推广费用ihanshiAndroid Studio 连接阿里云订阅主题然后使用JSON解析数据非常好用 导入MQTT的JAR包1、在项目中添加依赖然后使用Studio 去下载库2、直接下载JAR包#xff0c;然后作为库进行导入 环境验证#xff1a;给程序进行联网权限XML布局文件效果如下#xff1a; MainActitive.java 主… Android Studio 连接阿里云订阅主题然后使用JSON解析数据非常好用 导入MQTT的JAR包1、在项目中添加依赖然后使用Studio 去下载库2、直接下载JAR包然后作为库进行导入 环境验证给程序进行联网权限XML布局文件效果如下 MainActitive.java 主要代码如下End 导入MQTT的JAR包 这里有两种方法 1、在项目中添加依赖然后使用Studio 去下载库 2、直接下载JAR包然后作为库进行导入 这里先介绍第一种方法 切换到Android 目录 按照如下图添加依赖即可 implementation(org.eclipse.paho:mqtt-client:3-1.2.0)第二种方法 打开Android Studio 然后切换到Android 目录找到Lib文件夹将MQTT的JAR包复制进去即可。 然后点击project目录然后将MQTT JAR包导进去 添加完成之后作为库导入工程中
环境验证 在MainActivity 任取一个地方输入MQTT 看是否有提示弹窗。 到这里以及成功一半了。接下来就是设置一些权限问题 给程序进行联网权限 切换到按的Android 目录 找到如下的目录 添加如代码 uses-permission android:nameandroid.permission.INTERNET /!–允许程序获取网络状态–uses-permission android:nameandroid.permission.ACCESS_NETWORK_STATE /XML布局文件 ?xml version1.0 encodingutf-8? androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityLinearLayoutandroid:layout_widthmatch_parentandroid:background#51CCDCandroid:orientationverticalandroid:layout_heightmatch_parentLinearLayoutandroid:layout_widthmatch_parentandroid:background#673AB7android:layout_heightwrap_contentTextViewandroid:layout_widthmatch_parentandroid:text桂航果蔬种植基地android:textSize25spandroid:textColor#060A0Candroid:layout_heightmatch_parent/TextView/LinearLayoutLinearLayoutandroid:layout_widthwrap_contentandroid:layout_height200dpandroid:layout_gravitycenterandroid:layout_marginTop20dpandroidx.cardview.widget.CardViewandroid:layout_widthwrap_contentandroid:layout_height200dpandroid:layout_marginLeft10dpandroid:layout_marginRight10dpapp:cardCornerRadius20dpImageViewandroid:idid/m_img2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/bg3//androidx.cardview.widget.CardView/LinearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:background#51CCDCandroid:orientationverticalLinearLayoutandroid:layout_marginBottom3dpandroid:layout_marginLeft10dpandroid:layout_marginRight10dpandroid:layout_widthmatch_parentandroid:backgrounddrawable/shape_cornerandroid:layout_heightwrap_contentandroid:orientationverticalandroid:layout_marginTop5dpandroid:padding30dp!–左文字–TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text学号2020070230226android:textColorcolor/blackandroid:textSize16sp/TextView!–左文字–TextViewandroid:idid/m_mqttandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text 姓名xiao!android:textColorcolor/blackandroid:textSize16sp/TextView/LinearLayout/LinearLayout!– vertical 竖直排列 –!– horizontal 竖直排列 –!– 数据解析界面 –LinearLayoutandroid:layout_marginTop10dpandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:background#FFFF!– 包裹其他界面–LinearLayoutandroid:layout_margin5pxandroid:padding3dpandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:orientationverticalLinearLayoutandroid:layout_widthmatch_parentandroid:gravitycenterandroid:layout_weight1android:orientationhorizontalandroid:layout_heightmatch_parent!–气温–LinearLayoutandroid:layout_width160dpandroid:layout_weight1android:gravitycenterandroid:backgrounddrawable/shape_cornerandroid:layout_height105dpImageViewandroid:idid/testandroid:layout_width80dpandroid:layout_height80dpandroid:layout_marginTop10dpandroid:srcdrawable/air_tempandroid:layout_marginRight15dp/ImageViewLinearLayoutandroid:layout_widthwrap_contentandroid:orientationverticalandroid:layout_heightwrap_contentTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop5dpandroid:text温度android:textSize25sp/TextView!– 数值 –LinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextViewandroid:idid/air_tempandroid:layout_widthwrap_contentandroid:text 0 android:textSize25spandroid:layout_heightwrap_content/TextView/LinearLayout/LinearLayout/LinearLayout!–The End –!– 湿度–LinearLayoutandroid:layout_marginLeft25dpandroid:layout_width160dpandroid:layout_weight1android:gravitycenterandroid:backgrounddrawable/shape_cornerandroid:layout_height105dpImageViewandroid:layout_width80dpandroid:layout_height80dpandroid:layout_marginTop10dpandroid:srcdrawable/air_humiandroid:layout_marginRight15dp/ImageViewLinearLayoutandroid:layout_widthwrap_contentandroid:orientationverticalandroid:layout_heightwrap_contentTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop5dpandroid:text湿度android:textSize20sp/TextView!– 数值 –LinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextViewandroid:idid/air_humiandroid:layout_widthwrap_contentandroid:text 0 android:textSize25spandroid:layout_heightwrap_content/TextView/LinearLayout/LinearLayout/LinearLayout/LinearLayout!–The End –/LinearLayout!– 2 –LinearLayoutandroid:layout_widthmatch_parentandroid:background#FFFFandroid:layout_margin5pxandroid:padding3dpandroid:layout_heightwrap_content!– Layout1–LinearLayoutandroid:layout_widthmatch_parentandroid:gravitycenterandroid:layout_heightmatch_parent!–气温–!–Left1 –LinearLayoutandroid:layout_width160dpandroid:layout_weight1android:gravitycenterandroid:backgrounddrawable/shape_cornerandroid:layout_height105dpImageViewandroid:layout_width80dpandroid:layout_height80dpandroid:layout_marginTop10dpandroid:srcdrawable/soil_humiandroid:layout_marginRight15dp/ImageViewLinearLayoutandroid:layout_widthwrap_contentandroid:orientationverticalandroid:layout_heightwrap_contentTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop5dpandroid:text土壤湿度android:textSize20sp/TextView!– 数值 –LinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextViewandroid:idid/sloi_humiandroid:layout_widthwrap_contentandroid:text 0 android:textSize25spandroid:layout_heightwrap_content/TextView/LinearLayout/LinearLayout/LinearLayout!–The End –!– 湿度–LinearLayoutandroid:layout_width160dpandroid:layout_weight1android:gravitycenterandroid:layout_marginLeft25dpandroid:backgrounddrawable/shape_cornerandroid:layout_height105dpImageViewandroid:layout_width80dpandroid:layout_height80dpandroid:layout_marginTop10dpandroid:srcdrawable/n2android:layout_marginRight15dp/ImageViewLinearLayoutandroid:layout_widthwrap_contentandroid:orientationverticalandroid:layout_heightwrap_contentTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop5dpandroid:text土壤含氮android:textSize20sp/TextView!– 数值 –LinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextViewandroid:idid/soli_nandroid:layout_widthwrap_contentandroid:text 0 android:textSize25spandroid:layout_heightwrap_content/TextView/LinearLayout/LinearLayout/LinearLayout/LinearLayout/LinearLayout!– The End –!– 3 –LinearLayoutandroid:layout_margin5pxandroid:padding3dpandroid:layout_widthmatch_parentandroid:orientationhorizontalandroid:gravitycenterandroid:layout_heightwrap_contentLinearLayoutandroid:layout_width160dpandroid:backgrounddrawable/shape_cornerandroid:layout_weight1android:gravitycenterandroid:layout_height105dpImageViewandroid:layout_weight1android:layout_width80dpandroid:layout_height80dpandroid:layout_marginTop10dpandroid:srcdrawable/p2android:layout_marginRight15dp/ImageViewLinearLayoutandroid:layout_widthwrap_contentandroid:orientationverticalandroid:layout_heightwrap_contentTextViewandroid:layout_marginTop5dpandroid:layout_marginRight5dpandroid:layout_widthwrap_contentandroid:text土壤含磷android:textSize20spandroid:layout_heightwrap_content/TextViewTextViewandroid:idid/soil_pandroid:layout_marginTop5dpandroid:layout_widthwrap_contentandroid:text 0 android:textSize25spandroid:layout_heightwrap_content/TextView/LinearLayout/LinearLayout!–L1–LinearLayoutandroid:layout_marginLeft25dpandroid:gravitycenterandroid:layout_weight1android:layout_width160dpandroid:orientationhorizontalandroid:backgrounddrawable/shape_cornerandroid:layout_height105dpImageViewandroid:layout_weight1android:layout_width80dpandroid:layout_height80dpandroid:layout_marginTop10dpandroid:srcdrawable/k2android:layout_marginRight15dp/ImageViewLinearLayoutandroid:layout_widthwrap_contentandroid:orientationverticalandroid:layout_heightwrap_contentTextViewandroid:layout_marginRight5dpandroid:layout_widthwrap_contentandroid:text土壤含钾android:textSize20spandroid:layout_heightwrap_content/TextViewTextViewandroid:idid/soil_kandroid:layout_widthwrap_contentandroid:text 0 android:textSize25spandroid:layout_heightwrap_content/TextView/LinearLayout/LinearLayout!–Layout2–/LinearLayout!– The End –/LinearLayout/LinearLayout /androidx.constraintlayout.widget.ConstraintLayout效果如下 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 appSystem.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;Overrideprotected 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() {Overridepublic void connectionLost(Throwable throwable) {}Overridepublic 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() {Overridepublic 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() {Overridepublic void run() {AlertDialog.Builder builder new AlertDialog.Builder(MainActivity.this);builder.setTitle(MQTT消息);builder.setMessage(msg);builder.setPositiveButton(确定, new DialogInterface.OnClickListener() {Overridepublic 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();}});}Overridepublic void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {}});/点击事件/test.setOnClickListener(new View.OnClickListener() {Overridepublic 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); //设置心跳,30soptions.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 完整的代码工程可以进QQ群获取 764452437