概述
这篇文章对于大学学生来说应该是一个福利,因为可能跟你的毕业设计相关联。
1.GET 获取云平台的数据
private static final String DeviceID = "715410157";
private static final String ApiKey = "=0nEsfHhq5sfImFv4oQYGHv=wDg=";
private static final String humistream = "humistream";//onenet平台上对应设备的其中一个数据流的名字
private static final String tempstream = "tempstream";
public void Get() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient clientHumi = new OkHttpClient();
Request requestHumi = new Request.Builder().url("https://api.heclouds.com/devices/" + DeviceID + "/datapoints?datastream_id=" + humistream).header("api-key", ApiKey).build();
Response responseHumi = clientHumi.newCall(requestHumi).execute();
String responseHumiData = responseHumi.body().string();
parseJSONWithGSONHumi(responseHumiData);
} catch (IOException e) {
e.printStackTrace();
}
try {
OkHttpClient clientTemp = new OkHttpClient();
Request requestTemp = new Request.Builder().url("https://api.heclouds.com/devices/" + DeviceID + "/datapoints?datastream_id=" + tempstream).header("api-key", ApiKey).build();
Response responseTemp = clientTemp.newCall(requestTemp).execute();
String responseTempData = responseTemp.body().string();
parseJSONWithGSONTemp(responseTempData);
} catch (IOException e) {
e.printStackTrace();
}
try {
OkHttpClient clientAir = new OkHttpClient();
Request requestAir = new Request.Builder().url("https://api.heclouds.com/devices/" + DeviceID + "/datapoints?datastream_id=" + airQuality).header("api-key", ApiKey).build();
Response responseAir = clientAir.newCall(requestAir).execute();
String responseAirData = responseAir.body().string();
parseJSONWithGSONAir(responseAirData);
} catch (IOException e) {
e.printStackTrace();
}
try {
OkHttpClient clientLight = new OkHttpClient();
Request requestLight = new Request.Builder().url("https://api.heclouds.com/devices/" + DeviceID + "/datapoints?datastream_id=" + lightIntensities).header("api-key", ApiKey).build();
Response responseLight = clientLight.newCall(requestLight).execute();
String responseLightData = responseLight.body().string();
parseJSONWithGSONLight(responseLightData);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
想要获取正常的数据格式还需要数据解析,从云平台发下来的是json格式
接下来创建四个工具类
1.
```java
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 setvValue(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;
}
}
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;
}
}
private void parseJSONWithGSONAir(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 air = points.get(i).getValue();
runOnUiThread(new Runnable() {
@Override
public void run() {
//下面是你需要展示数据界面的操作,不要什么都不想,直接复制,我之所以发这篇文章是想,如果你对安卓确实不熟悉,可以给你帮助,但如果你想了解安卓或从事相关行业,那应该以后抽时间把它看懂。
airQualityData.setText(air);
if (Integer.parseInt(air.toString().trim()) > 65) {
airQualityData.setTextColor(Color.parseColor("#ff2400"));
} else if (Integer.parseInt(air.toString()) < 45) {
airQualityData.setTextColor(Color.parseColor("#238e23"));
} else {
airQualityData.setTextColor(Color.parseColor("#7f7f7f"));
}
}
});
}
}
对于上面的参数我不会跟你说,因为你能够搜索到这篇文章说明,说明你应该对上面的参数熟悉,而且我的参数命名已经很清楚了。
2.发送数据到云平台(这里是发送数据不是发送命令)
private fun sendLight(id: String, value: Boolean) {
Thread {
//
var connection: HttpURLConnection? = null
var reader: BufferedReader? = null
try {
//这里的724410117是你的设备号
val url = URL("https://api.heclouds.com/devices/724410117/datapoints")
connection = url.openConnection() as HttpURLConnection
//connection.setRequestMethod("GET");//如需使用post方式,注释本行,释放下面三行
//POST方式,传递数据
connection.connectTimeout = 5000
connection!!.doOutput = true
connection.doInput = true
connection.requestMethod = "POST"
connection.useCaches = false
val data =
"{"datastreams": [{"id":"" + id + "","datapoints": [{"value":" +
value + "}]}]}"
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded ")
connection.setRequestProperty("Content-Len", data.length.toString() + "")
connection.setRequestProperty("api-key", ApiKey)
connection.readTimeout = 8000
// DataOutputStream data1 = new DataOutputStream(connection.getOutputStream());
val pw = PrintWriter(connection.outputStream)
pw.print(data)
pw.flush()
pw.close()
val inputStream = connection.inputStream //获取服务器返回输入流
reader = BufferedReader(InputStreamReader(inputStream))
val response = StringBuilder()
var line: String?
while (reader.readLine().also { line = it } != null) {
response.append(line)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
if (reader != null) {
try {
reader.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
connection?.disconnect()
}
}.start()
}
这里你通过调用sendLight方法传入数据流的名称和值就好了。
其他的问题可以点击下面的链接去查看官方文档
云平台文档中心
最后
以上就是漂亮豆芽为你收集整理的android与OneNET云平台的获取数据与发送数据的全部内容,希望文章能够帮你解决android与OneNET云平台的获取数据与发送数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复