016-Android获取onenet平台上的数据

厌伴老儒烹瓠叶,强随举子踏槐花。这篇文章主要讲述016-Android获取onenet平台上的数据相关的知识,希望能为你提供帮助。

package com.example.webview; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity {private String msg; //声明一个输出值 private Button getmsg; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getmsg = (Button) findViewById(R.id.getmsg); getmsg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new newThread().start(); //方法1:直接创建子线程并启动 //new Thread(){ //@Override //public void run() { ////第一步:实例化URL对象 //String address="https://api.heclouds.com/devices/584758432/datastreams/dust_concentration"; //查询最新数据流详情 ////String address="https://api.heclouds.com/devices/584758432/datapoints?limit=10"; //查询历史数据 ////String address="https://api.heclouds.com/devices/584758432/datastreams/dust_concentration"; //查询数据流详情 // //try { //URL url =new URL(address); //实例化URL对象 ////实例化 HttpURLConnection对象 //HttpURLConnection conn = (HttpURLConnection) url.openConnection(); ////设置链接属性 //conn.setRequestMethod("GET"); //设置请求方法 //conn.setRequestProperty("api-key","TeO0juJlw1uDTmDJRKD7cvzj=cM="); //conn.setRequestProperty("Host","api.heclouds.com"); //conn.setReadTimeout(5000); //设置超时时间 //if(conn.getResponseCode()==200){ //获取状态码 200表示连接成功 ////获取输入流 //InputStream in= conn.getInputStream(); ////读取输入流 //byte[] b=new byte[1024*512]; //定义一个byte数组读取输入流 //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据 //int len=0; //while((len=in.read(b))> -1){//每次读的len> -1 说明是是有数据的 //baos.write(b,0,len); //三个参数输入流byte数组读取起始位置读取终止位置 //} //String msg=baos.toString(); //Log.e("TAG",msg); // //} //} catch (java.io.IOException e) { //e.printStackTrace(); //} //} //}.start(); ////方法2:先创建子线程,然后启动 ////private Thread newThread; //声明一个子线程 //newThread = new Thread(new Runnable() { //@Override //public void run() { ////第一步:实例化URL对象 //String address="https://api.heclouds.com/devices/584758432/datastreams/dust_concentration"; //查询最新数据流详情 ////String address="https://api.heclouds.com/devices/584758432/datapoints?limit=10"; //查询历史数据 ////String address="https://api.heclouds.com/devices/584758432/datastreams/dust_concentration"; //查询数据流详情 // //try { //URL url =new URL(address); //实例化URL对象 ////实例化 HttpURLConnection对象 //HttpURLConnection conn = (HttpURLConnection) url.openConnection(); ////设置链接属性 //conn.setRequestMethod("GET"); //设置请求方法 //conn.setRequestProperty("api-key","TeO0juJlw1uDTmDJRKD7cvzj=cM="); //conn.setRequestProperty("Host","api.heclouds.com"); //conn.setReadTimeout(5000); //设置超时时间 //if(conn.getResponseCode()==200){ //获取状态码 200表示连接成功 ////获取输入流 //InputStream in= conn.getInputStream(); ////读取输入流 //byte[] b=new byte[1024*512]; //定义一个byte数组读取输入流 //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据 //int len=0; //while((len=in.read(b))> -1){//每次读的len> -1 说明是是有数据的 //baos.write(b,0,len); //三个参数输入流byte数组读取起始位置读取终止位置 //} //String msg=baos.toString(); //Log.e("TAG",msg); // //} //} catch (java.io.IOException e) { //e.printStackTrace(); //} //} //}); //newThread.start(); } }); }//方法3:创建外部线程类 //public classnewThread extends Thread{ //public void run(){ ////第一步:实例化URL对象 //String address="https://api.heclouds.com/devices/584758432/datastreams/dust_concentration"; //查询最新数据流详情 ////String address="https://api.heclouds.com/devices/584758432/datapoints?limit=10"; //查询历史数据 ////String address="https://api.heclouds.com/devices/584758432/datastreams/dust_concentration"; //查询数据流详情 // //try { //URL url =new URL(address); //实例化URL对象 ////实例化 HttpURLConnection对象 //HttpURLConnection conn = (HttpURLConnection) url.openConnection(); ////设置链接属性 //conn.setRequestMethod("GET"); //设置请求方法 //conn.setRequestProperty("api-key","TeO0juJlw1uDTmDJRKD7cvzj=cM="); //conn.setRequestProperty("Host","api.heclouds.com"); //conn.setReadTimeout(5000); //设置超时时间 //if(conn.getResponseCode()==200){ //获取状态码 200表示连接成功 ////获取输入流 //InputStream in= conn.getInputStream(); ////读取输入流 //byte[] b=new byte[1024*512]; //定义一个byte数组读取输入流 //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据 //int len=0; //while((len=in.read(b))> -1){//每次读的len> -1 说明是是有数据的 //baos.write(b,0,len); //三个参数输入流byte数组读取起始位置读取终止位置 //} //String msg=baos.toString(); //Log.e("TAG",msg); //} //} catch (java.io.IOException e) { //e.printStackTrace(); //} //} //}}

【016-Android获取onenet平台上的数据】 

    推荐阅读