一、在onenet创建产品
onenet的地址
1、创建http产品
选择http——>点击网址右上角的增加产品
2、创建产品成功后立即增加设备
3、设备有了之后增加数据流
4、然后在增加的数据流随便模拟数据
还不知怎么操作的可以看下别人方法
二、在android中查询onenet的数据流
1、具体方法:别人的
2、该方法有个问题:在运行下面语句时会出现网络请求失败CLEARTEXT communication to “ “ not permitted by network security policy
Response response = client.newCall(request).execute();
解决方法网址:别人的
3、该方面的解析json是用gson
JsonRootBean app = new Gson().fromJson(jsonData, JsonRootBean.class);
jsonData的数据是
{"errno":0,"data":{"count":1,"datastreams":[{"datapoints":[{"at":"2020-08-15 10:00:06.587","value":"12"}],"id":"light"}]},"error":"succ"}
你只要一层一层的创建好类,和类中的变量与jsonData的数据名一样就可以了,gson会自动解析掉
三、更新onenet数据流的数据
弄时先看http协议,必看
data利用简便写法
public void Pos(){
new Thread(new Runnable() {
@Override
public void run() {
try{
OkHttpClient client = new OkHttpClient();
// 数据格式从官方文档看,type用5情况
String data=new String(",;light,12");
//发送type写法
RequestBody requestBody = RequestBody.create( data,MediaType.get("application/json"));
Request request = new Request.Builder()
.url("http://api.heclouds.com/devices/" + DeviceID + "/datapoints?type=5")
.post(requestBody)
.header("api-key", ApiKey)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
//若输出error:0则更新成功
Log.w("www",responseData);
}catch (Exception e){
Log.d("www",e.getMessage());
Log.d("www",e.getLocalizedMessage());
}
}
}).start();
}
data一般写法
String data = new String("{\"datastreams\":[{\"datapoints\":[{\"value\":\"20\"}],\"id\":\"light\"}]}");