赞
踩
使用android向onenet物联网云平台请求数据
在manifest加入<uses-permission android:name="android.permission.INTERNET"/>,如图所示:
在xml文件夹中创建一个名为“network_security_config”的xml文件,如图所示:
其内容为:
- <?xml version="1.0" encoding="utf-8"?>
- <network-security-config>
- <base-config cleartextTrafficPermitted="true">
- <trust-anchors>
- <certificates src="system" />
- </trust-anchors>
- </base-config>
- </network-security-config>
由于本人是用okhttp进行网络请求的,所以需要在build.gradle中加入此句:
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
okhttp有很多版本,最新版可通过其官网查看Overview - OkHttp (square.github.io)
上代码!!
package com.example.???; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import java.io.IOException; import java.util.Arrays; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; //if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } public class MainActivity extends AppCompatActivity { private TextView textView1; private TextView textView2; private TextView textView3; private TextView textView4; private Button button; private String[] name = new String[4]; private String[] val = new String[4]; private String DEVICE_ID = "设备id"; private String PRODUCT_ID = "产品id"; private String APIKey = "设备的key"; private String PRODUCT_KEY = "产品的key"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { s(); } }); } public void s() { //同步请求 // new Thread(new Runnable() { // @RequiresApi(api = Build.VERSION_CODES.O) // @Override // public void run() { // try { // OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象 // Request request = new Request.Builder() // .url(String.format("http://api.heclouds.com/devices/%s/datapoints?",DEVICE_ID ))//请求接口。如果需要传参拼接到接口后面。 // .addHeader("Authorization","填入你的token,注意填入的token不要超过登陆限制的时长,否则失败") // .build();//创建Request 对象 // Response response = null; // response = client.newCall(request).execute();//得到Response 对象 // if (response.body() != null) { // String data = response.body().string(); // JSONObject jsonObject = new JSONObject(data); // Log.d("kwwl","response.code()=="+response.code()); // Log.d("kwwl","response.message()=="+response.message()); Log.d("kwwl","res=="+response.body().string()); // Log.d("kwwl","res=="+data); // Log.d("kwwl","type of body"+data.getClass().getTypeName()); Log.d("kwwl", (String) jsonObject.get("errno")); // // textView = findViewById(R.id.textview1); // textView.setText(data); // } // } catch (Exception e) { // e.printStackTrace(); // } // } // }).start(); //异步请求 new Thread(new Runnable() { @Override public void run() { OkHttpClient client = new OkHttpClient(); FormBody.Builder form = new FormBody.Builder(); form.add("led_state", "1"); Request request = new Request.Builder() .url(String.format("http://api.heclouds.com/devices/%s/datapoints?", DEVICE_ID))//请求接口。如果需要传参拼接到接口后面。 .addHeader("Authorization", "你的token") .build();//创建Request 对象 client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.body() != null) { String result = response.body().string(); Log.d("kwwl", "message==" + response.message()); Log.d("kwwl", "code==" + response.code()); Log.d("kwwl", "data==" + result); try { JSONObject jsonObject = new JSONObject(result); JSONObject data = jsonObject.getJSONObject("data"); JSONArray datasteams = data.getJSONArray("datastreams"); for (int i = 0; i < datasteams.length(); i++) { JSONObject indexObj = datasteams.getJSONObject(i); String id = indexObj.optString("id"); name[i] = id; JSONArray datapoints = indexObj.getJSONArray("datapoints"); JSONObject valueB = datapoints.getJSONObject(0); String value = valueB.optString("value"); Log.d("kwwl", "value====" + value); val[i] = value; } } catch (JSONException e) { e.printStackTrace(); } for (int i = 0; i < name.length; i++) { Log.d("kwwl", "类型:" + name[i] + "\t" + "数值:" + val[i]); } textView1 = findViewById(R.id.textview1); textView2 = findViewById(R.id.textview2); textView3 = findViewById(R.id.textview3); textView4 = findViewById(R.id.textview4); String str1 = "类型:" + name[0] + "\t" + "数值:" + val[0]; textView1.setText(str1); String str2 = "类型:" + name[1] + "\t" + "数值:" + val[1]; textView2.setText(str2); String str3 = "类型:" + name[2] + "\t" + "数值:" + val[2]; textView3.setText(str3); String str4 = "类型:" + name[3] + "\t" + "数值:" + val[3]; textView4.setText(str4); } } }); } }).start(); } }
onenet平台中,如果设备接入的是mqtts协议,那么想要对其发起数据请求,貌似只能通过Authorization的方式进行数据请求,但官方文档说除了此方式外还可以通过apk-key的方式也行,但是本人试了不行,而且后面客服反馈说如果是mqtts协议,就只能Authorization的方式请求,官方文档给出的东西还是有坑的,小伙伴们记得闭坑,不知道啥时候才能改过来。
除此之外,以上还提到了token,我们可以通过使用官方给出的代码生成自己的token,连接如下:
python_开发者文档_OneNET (10086.cn)
一个是java的代码另一个则是python的,大家自行选择。(注意:官方还推出了token计算工具,在此嘱咐大家别用这个工具,因为使用这个工具生成的token是请求不到数据的,所以大家还是用代码吧,代码也挺香的)
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请求数据" android:id="@+id/button1" /> <TextView android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="100dp" android:text="textview" android:textSize="22sp" /> <TextView android:id="@+id/textview2" android:layout_width="match_parent" android:layout_height="100dp" android:text="textview" android:textSize="22sp" /> <TextView android:id="@+id/textview3" android:layout_width="match_parent" android:layout_height="100dp" android:text="textview" android:textSize="22sp" /> <TextView android:id="@+id/textview4" android:layout_width="match_parent" android:layout_height="100dp" android:text="textview" android:textSize="22sp" /> </LinearLayout>
点一下button按钮后稍等一段时间
注:以上代码只有一个MainActivity和activity_main.xml文件,结构很简单,主要是想实现一个通过android开发工具实现对onenet物联网云平台设备数据的请求。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。