##Android App 连接onenet获取温湿度实例
创建一个新project
先添加一个button和三个TextView(分别是温度,湿度,时间)
把温度和湿度两个TextView拉大点,用来显示数据。
TextView的位置随便放。
先去onenet官网查看自己账号的api
根据这里修改获取数据的URL
再利用Apipost软件进行连接测试获取数据(点击此处下载)
在MainActivity.java中添加代码块
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(