##Android App 连接onenet获取温湿度实例
把温度和湿度两个TextView拉大点,用来显示数据。
TextView的位置随便放。
再利用Apipost软件进行连接测试获取数据(点击此处下载)
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.example.test1.util.Json;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
static String time;//时间
static Double wendu;//温度
static Double shidu;//湿度
//onenet连接的api
public final String url1 = "http://api.heclouds.com/devices/629063002/datapoints?datastream_id=3303_0_5700&limit=5";
//device/填写自己的设备ID,
//datastream_id填写自己的nbiot相应的onenet里温度湿度的数据
public final String url2 = "http://api.heclouds.com/devices/629063002/datapoints?datastream_id=3304_0_5700&limit=5";
private TextView txtwendu,txtshidu,shijian,txtwendu1,txtshidu1;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtwendu = findViewById(R.id.txtwendu);
txtshidu = findViewById(R.id.txtshidu);
btn = findViewById(R.id.kaiqi);
shijian = findViewById(R.id.shijian);
txtwendu1 = findViewById(R.id.txtwendu1);
txtshidu1 = findViewById(R.id.txtshidu1);
//开启应用按钮点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getData();
}
});
}
public void getData(){
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url1)
.addHeader("api-key", "LoEEarS4lSeRRLeuwtlK19arUlY=")
.addHeader("Content-Type","application/json")
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
Log.w("test", responseData);
//json提取数据
JsonRootBean app = Json.gson.fromJson(responseData, JsonRootBean.class);
List<Datastreams> streams = app.getData().getDatastreams();
List<Datapoints> points = streams.get(0).getDatapoints();
int count = app.getData().getCount();//获取数据的数量
// wendu = new Integer(1);
wendu = points.get(1).getValue();
Log.w("www","wendu="+wendu);//log输出
Log.w("www","shijian="+points.get(1).getAt().toString());
final String wendu1 = wendu.toString();//数据类型转换
//textview显示需要回到主线程
txtwendu1.post(new Runnable() {
@Override
public void run() {
txtwendu.setText("当前温度:");
}
});
txtwendu.post(new Runnable() {
@Override
public void run() {
txtwendu.setText("当前温度:"+String.format("%.1f°C",wendu));
}
});
time = new String();
time = points.get(1).getAt().toString();
Log.w("www","shijian="+time);
// final String time1 =time.substring(0,19);
shijian.post(new Runnable() {
@Override
public void run() {
shijian.setText(" 时间: "+time);
// shijian.setText(" 时间: "+time1);
}
});
}catch (IOException e){
e.printStackTrace();
}
try {
OkHttpClient client1 = new OkHttpClient();
Request request1 = new Request.Builder()
.url(url2)
.addHeader("api-key", "LoEEarS4lSeRRLeuwtlK19arUlY=")
.addHeader("Content-Type","application/json")
.build();
Response response1 = client1.newCall(request1).execute();
String responseData1 = response1.body().string();
JsonRootBean app1 = Json.gson.fromJson(responseData1, JsonRootBean.class);
List<Datastreams> streams1 = app1.getData().getDatastreams();
List<Datapoints> points1 = streams1.get(0).getDatapoints();
int count = app1.getData().getCount();//获取数据的数量
shidu = points1.get(1).getValue();
Log.w("www","shidu="+shidu);
final String shidu1 = shidu.toString();
txtshidu1.post(new Runnable() {
@Override
public void run() {
txtshidu.setText("当前湿度:");
}
});
txtshidu.post(new Runnable() {
@Override
public void run() {
txtshidu.setText("当前湿度:"+String.format("%.1f%%",shidu));
}
});
}catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.google.code.gson:gson:2.7'
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
implementation 'com.google.android.material:material:1.1.0'
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
//在application里面添加
android:supportsRtl="true"
<!-- Mqtt服务 -->
<service android:name="org.eclipse.paho.android.service.MqttService" />
public class Datapoints {
private Date at;
private double value;
public void setAt(Date at) {
this.at = at;
}
public Date getAt() {
return at;
}
public void setValue(double value) {
this.value = value;
}
public double 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;
}
}
这里显示两个例子其他的自己根据转换工具添加。
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Json {
public static Gson gson;
static {
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
}
}
做到这里就已经做好onenet 连接 Android app