使用Java获取OneNet平台数据

使用Java获取OneNet平台数据----笔记2

OneNet平台提供了API使用方法,但是部分小白可能看不懂具体怎么使用
OneNet提供了API使用方法
在这里插入图片描述
doGet(“http://api.heclouds.com/devices/{devices_id}/datapoints?datastream_id={humidity}&start=2017-01-01T00:00:00&limit=100”);

这里是获取设备历史数据的例子,只需将URL中的devices_iddatastream_id和代码中token改为自己的就行。

注意:官方网址最后的HTTP/1.1不加

token算法
token计算工具下载及使用方法

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/*import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;*/
public class getclass {
    public static void main(String[] args) {
        //调用方法
        doGet("http://api.heclouds.com/devices/{devices_id}/datapoints?datastream_id={humidity}&start=2017-01-01T00:00:00&limit=100");
    }
    public static String doGet(String url) {
        HttpURLConnection httpURLConnection = null;
        InputStream in = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            // 创建远程url连接对象
            URL url1 = new URL(url);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            httpURLConnection = (HttpURLConnection) url1.openConnection();
            // 设置连接方式:get
            httpURLConnection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            httpURLConnection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            httpURLConnection.setReadTimeout(60000);
            //设置格式
            httpURLConnection.setRequestProperty("Content-type", "application/json");
            // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 OneNet平台使用的是Authorization+token
            httpURLConnection.setRequestProperty("authorization","你的token");
            // 发送请求
            httpURLConnection.connect();
            // 通过connection连接,获取输入流
            if (httpURLConnection.getResponseCode() == 200) {
                in = httpURLConnection.getInputStream();
                // 封装输入流is,并指定字符集
                br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
                System.out.println("response="+result);
                //处理返回的数据
                /*JSONObject jsonObject = JSON.parseObject(result);
                System.out.println("jsonobj="+jsonObject);
                JSONObject jsonObject1=jsonObject.getJSONObject("data");
//                String datastreams = jsonObject1.getString("datastreams");
//                System.out.println("datastreams中的"+datastreams);
                JSONArray array=jsonObject1.getJSONArray("datastreams");
                JSONObject jsonObject2=array.getJSONObject(0);
                System.out.println("datastreams内的="+jsonObject2);
                JSONArray array1=jsonObject2.getJSONArray("datapoints");
                for(int i=0;i<array1.size();i++){
                    JSONObject jsonObject4=array1.getJSONObject(i);
                    String time=jsonObject4.get("at").toString();
                    String values=jsonObject4.get("value").toString();
                    System.out.println("时间为 "+time.toString()+" and "+" 湿度为 "+values.toString());
                }*/
            }
        } catch (UnsupportedEncodingException unsupportedEncodingException) {
            unsupportedEncodingException.printStackTrace();
        } catch (ProtocolException protocolException) {
            protocolException.printStackTrace();
        } catch (MalformedURLException malformedURLException) {
            malformedURLException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            httpURLConnection.disconnect();// 关闭远程连接
        }
        return result;

        }

请求成功后会返回一个json格式的字符串,例:
response={“errno”:0,“data”:{“count”:21,“datastreams”:[{“datapoints”:[{“at”:“2021-05-21 13:04:56.257”,“value”:“85”},{“at”:“2021-05-21 13:05:35.199”,“value”:“85L”}]},“error”:“succ”}
如果要获取其中的数据需要现将其转化为json对象,代码中有操作方法这里就不多赘述,需注意的是如果要使用代码中操作json的方法需导入处理fastjson-1.2.76.jar包下载地址

public class test {
    public static void main(String[] args) {
        Gettest("http://api.heclouds.com/devices/722413660/datastreams/level");
    }
    public static String Gettest(String url) {
        HttpURLConnection connection = null;
        InputStream in = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            URL url1=new URL(url);
            connection = (HttpURLConnection) url1.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            //设置格式
            connection.setRequestProperty("Content-type", "application/json");
            //设置验证方式
            connection.setRequestProperty("authorization", "version=2018-10-31&res=products%2F430138&et=1672735919&method=md5&sign=RTtOqY83oqgwlSYU5DPiQA%3D%3D");
            //也可以
           // connection.setRequestProperty("api-key","你的Master-APIkey");
            in = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(in));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                response.append(line);
            }
            result=response.toString();
            System.out.println(response);

        } catch (MalformedURLException | ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return result;
    }
    }

后续可以看看我的另一篇文章,使用HttpClient发送请求,更加的简洁方便HttpClient的基本使用

https://www.cnblogs.com/hhhshct/p/8523697.html java实现HTTP请求的三种方式

  • 9
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值