概述
onenet平台上:
设备信息:主要是设备id和APIKey这两个参数
数据流信息:
其中一个数据流的名称:shumditity
最新一个数据:
参考:https://www.arduino.cn/thread-47375-1-2.html
所有数据流的最新一个数据:
"http://api.heclouds.com/devices/36427574/datapoints"
一个数据流中所有数据(指定获取的开始时间):
"http://api.heclouds.com/devices/36427574/datapoints?datastream_id=command1&start=2018-10-12T17:22:33"
虽然指定了时间,但是只能显示100条。可以用limit=120来显示(limit:参与运算的数据点最大数量,默认6000,最大6000):
"http://api.heclouds.com/devices/*****/datapoints?datastream_id=temperature&start=2018-10-12T17:22:33&limit=120”
代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends Activity {
private static final String DeviceID = "36326052";
private static final String ApiKey = "A1RUKhwKwfTGf0gKhY6bHOz4DOw=";
private static final String shumditity="shumditity";//onenet平台上对应设备的其中一个数据流的名字
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Get();
}
public void Get() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://api.heclouds.com/devices/" + DeviceID + "/datapoints?datastream_id=" + shumditity).header("api-key", ApiKey).build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseJSONWithGSON(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseJSONWithGSON(String jsonData) {
JsonRootBean app = new Gson().fromJson(jsonData, JsonRootBean.class);
List<Datastreams> streams = app.getData().getDatastreams();
List<Datapoints> points = streams.get(0).getDatapoints();
int count = app.getData().getCount();//获取数据的数量
for (int i = 0; i < points.size(); i++) {
String time = points.get(i).getAt();
String value = points.get(i).getValue();
Log.w("www","time="+time);
Log.w("www","value="+value);
}
}
}
打印结果:
在build.gradle中增加:
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.google.code.gson:gson:2.7'
在AndroidManifest.xml中添加:
<uses-permission android:name="android.permission.INTERNET" />
json解析:
json 在线转换工具:http://www.bejson.com/
public class JsonRootBean {
private int errno;
private Data data;
private String error;
public void setErrno(int errno) {
this.errno = errno;
}
public int getErrno() {
return errno;
}
public void setData(Data data) {
this.data = data;
}
public Data getData() {
return data;
}
public void setError(String error) {
this.error = error;
}
public String getError() {
return error;
}
}
public class Data {
private int count;
private List<Datastreams> datastreams;
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public void setDatastreams(List<Datastreams> datastreams) {
this.datastreams = datastreams;
}
public List<Datastreams> getDatastreams() {
return datastreams;
}
}
public class Datapoints {
private String at;
private String value;
public void setValue(String value) {
this.value = value;
}
public String getAt() {
return at;
}
public void setAt(String at) {
this.at = at;
}
public String getValue() {
return value;
}
}
public class Datastreams {
private List<Datapoints> datapoints;
private String id;
public void setDatapoints(List<Datapoints> datapoints) {
this.datapoints = datapoints;
}
public List<Datapoints> getDatapoints() {
return datapoints;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
最后
以上就是朴素小霸王为你收集整理的Android从onenet上获取数据的全部内容,希望文章能够帮你解决Android从onenet上获取数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复