当前位置:   article > 正文

安卓android向onenet物联网云平台请求数据(接入协议mqtts)_ontnet android上云

ontnet android上云

使用android向onenet物联网云平台请求数据

第一步:

在manifest加入<uses-permission android:name="android.permission.INTERNET"/>,如图所示:

第二步:

在xml文件夹中创建一个名为“network_security_config”的xml文件,如图所示:

其内容为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <network-security-config>
  3. <base-config cleartextTrafficPermitted="true">
  4. <trust-anchors>
  5. <certificates src="system" />
  6. </trust-anchors>
  7. </base-config>
  8. </network-security-config>

第三步:

由于本人是用okhttp进行网络请求的,所以需要在build.gradle中加入此句:

implementation 'com.squareup.okhttp3:okhttp:3.10.0'

okhttp有很多版本,最新版可通过其官网查看Overview - OkHttp (square.github.io)

第四部:

上代码!!

  1. package com.example.???;
  2. import androidx.annotation.RequiresApi;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import okhttp3.Call;
  7. import okhttp3.Callback;
  8. import okhttp3.FormBody;
  9. import okhttp3.OkHttpClient;
  10. import okhttp3.Request;
  11. import okhttp3.Response;
  12. import android.os.Build;
  13. import android.os.Bundle;
  14. import android.os.StrictMode;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.TextView;
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22. //if (android.os.Build.VERSION.SDK_INT > 9) {
  23. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  24. StrictMode.setThreadPolicy(policy);
  25. }
  26. public class MainActivity extends AppCompatActivity {
  27. private TextView textView1;
  28. private TextView textView2;
  29. private TextView textView3;
  30. private TextView textView4;
  31. private Button button;
  32. private String[] name = new String[4];
  33. private String[] val = new String[4];
  34. private String DEVICE_ID = "设备id";
  35. private String PRODUCT_ID = "产品id";
  36. private String APIKey = "设备的key";
  37. private String PRODUCT_KEY = "产品的key";
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. button = findViewById(R.id.button1);
  43. button.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View view) {
  46. s();
  47. }
  48. });
  49. }
  50. public void s() {
  51. //同步请求
  52. // new Thread(new Runnable() {
  53. // @RequiresApi(api = Build.VERSION_CODES.O)
  54. // @Override
  55. // public void run() {
  56. // try {
  57. // OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
  58. // Request request = new Request.Builder()
  59. // .url(String.format("http://api.heclouds.com/devices/%s/datapoints?",DEVICE_ID ))//请求接口。如果需要传参拼接到接口后面。
  60. // .addHeader("Authorization","填入你的token,注意填入的token不要超过登陆限制的时长,否则失败")
  61. // .build();//创建Request 对象
  62. // Response response = null;
  63. // response = client.newCall(request).execute();//得到Response 对象
  64. // if (response.body() != null) {
  65. // String data = response.body().string();
  66. // JSONObject jsonObject = new JSONObject(data);
  67. // Log.d("kwwl","response.code()=="+response.code());
  68. // Log.d("kwwl","response.message()=="+response.message());
  69. Log.d("kwwl","res=="+response.body().string());
  70. // Log.d("kwwl","res=="+data);
  71. // Log.d("kwwl","type of body"+data.getClass().getTypeName());
  72. Log.d("kwwl", (String) jsonObject.get("errno"));
  73. //
  74. // textView = findViewById(R.id.textview1);
  75. // textView.setText(data);
  76. // }
  77. // } catch (Exception e) {
  78. // e.printStackTrace();
  79. // }
  80. // }
  81. // }).start();
  82. //异步请求
  83. new Thread(new Runnable() {
  84. @Override
  85. public void run() {
  86. OkHttpClient client = new OkHttpClient();
  87. FormBody.Builder form = new FormBody.Builder();
  88. form.add("led_state", "1");
  89. Request request = new Request.Builder()
  90. .url(String.format("http://api.heclouds.com/devices/%s/datapoints?", DEVICE_ID))//请求接口。如果需要传参拼接到接口后面。
  91. .addHeader("Authorization", "你的token")
  92. .build();//创建Request 对象
  93. client.newCall(request).enqueue(new Callback() {
  94. @Override
  95. public void onFailure(Call call, IOException e) {
  96. }
  97. @Override
  98. public void onResponse(Call call, Response response) throws IOException {
  99. if (response.body() != null) {
  100. String result = response.body().string();
  101. Log.d("kwwl", "message==" + response.message());
  102. Log.d("kwwl", "code==" + response.code());
  103. Log.d("kwwl", "data==" + result);
  104. try {
  105. JSONObject jsonObject = new JSONObject(result);
  106. JSONObject data = jsonObject.getJSONObject("data");
  107. JSONArray datasteams = data.getJSONArray("datastreams");
  108. for (int i = 0; i < datasteams.length(); i++) {
  109. JSONObject indexObj = datasteams.getJSONObject(i);
  110. String id = indexObj.optString("id");
  111. name[i] = id;
  112. JSONArray datapoints = indexObj.getJSONArray("datapoints");
  113. JSONObject valueB = datapoints.getJSONObject(0);
  114. String value = valueB.optString("value");
  115. Log.d("kwwl", "value====" + value);
  116. val[i] = value;
  117. }
  118. } catch (JSONException e) {
  119. e.printStackTrace();
  120. }
  121. for (int i = 0; i < name.length; i++) {
  122. Log.d("kwwl", "类型:" + name[i] + "\t" + "数值:" + val[i]);
  123. }
  124. textView1 = findViewById(R.id.textview1);
  125. textView2 = findViewById(R.id.textview2);
  126. textView3 = findViewById(R.id.textview3);
  127. textView4 = findViewById(R.id.textview4);
  128. String str1 = "类型:" + name[0] + "\t" + "数值:" + val[0];
  129. textView1.setText(str1);
  130. String str2 = "类型:" + name[1] + "\t" + "数值:" + val[1];
  131. textView2.setText(str2);
  132. String str3 = "类型:" + name[2] + "\t" + "数值:" + val[2];
  133. textView3.setText(str3);
  134. String str4 = "类型:" + name[3] + "\t" + "数值:" + val[3];
  135. textView4.setText(str4);
  136. }
  137. }
  138. });
  139. }
  140. }).start();
  141. }
  142. }

onenet平台中,如果设备接入的是mqtts协议,那么想要对其发起数据请求,貌似只能通过Authorization的方式进行数据请求,但官方文档说除了此方式外还可以通过apk-key的方式也行,但是本人试了不行,而且后面客服反馈说如果是mqtts协议,就只能Authorization的方式请求,官方文档给出的东西还是有坑的,小伙伴们记得闭坑,不知道啥时候才能改过来。

除此之外,以上还提到了token,我们可以通过使用官方给出的代码生成自己的token,连接如下:

java_开发者文档_OneNET (10086.cn)

python_开发者文档_OneNET (10086.cn)

一个是java的代码另一个则是python的,大家自行选择。(注意:官方还推出了token计算工具,在此嘱咐大家别用这个工具,因为使用这个工具生成的token是请求不到数据的,所以大家还是用代码吧,代码也挺香的)

  1. <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <Button
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="请求数据"
  11. android:id="@+id/button1"
  12. />
  13. <TextView
  14. android:id="@+id/textview1"
  15. android:layout_width="match_parent"
  16. android:layout_height="100dp"
  17. android:text="textview"
  18. android:textSize="22sp"
  19. />
  20. <TextView
  21. android:id="@+id/textview2"
  22. android:layout_width="match_parent"
  23. android:layout_height="100dp"
  24. android:text="textview"
  25. android:textSize="22sp"
  26. />
  27. <TextView
  28. android:id="@+id/textview3"
  29. android:layout_width="match_parent"
  30. android:layout_height="100dp"
  31. android:text="textview"
  32. android:textSize="22sp"
  33. />
  34. <TextView
  35. android:id="@+id/textview4"
  36. android:layout_width="match_parent"
  37. android:layout_height="100dp"
  38. android:text="textview"
  39. android:textSize="22sp"
  40. />
  41. </LinearLayout>

效果:

点一下button按钮后稍等一段时间

注:以上代码只有一个MainActivity和activity_main.xml文件,结构很简单,主要是想实现一个通过android开发工具实现对onenet物联网云平台设备数据的请求。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/432228
推荐阅读
相关标签
  

闽ICP备14008679号