get方式从OneNET平台查询设备信息

使用GET请求获取OneNET平台设备详情
本文详细介绍了如何通过HTTP GET方法从OneNET物联网平台获取设备的详细信息,包括步骤、所需API及参数设置,帮助开发者更好地进行设备管理和数据交互。
package com.how2java.test;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * @author :liyou
 * @date :Created in 2019/8/10 14:07
 * @description:测试从OneNET平台上获取数据
 * @modified By:
 * @version: 1.0$
 */
public class test03DataFromOneNET {

    /**
     * create by: liyou
     * description: 所有API的调用都需要在请求中加入请求头:api-key才可以访问到API接口
     *              api-key: mnJDvPM=MIiy8dmXI719R6Ixuww=
     * create time: 2019-08-10 14:19
     *
     * @return a
     * @Param: nul
     */
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(test03DataFromOneNET.class);
        System.out.println("程序开始了");
        String url = "http://api.heclouds.com/devices/538807312";
        String apiKey = "api-key";
        String apiValue = "mnJDvPM=MIiy8dmXI719R6Ixuww=";
//        执行查询单个设备的方法获得String对象并输出
        String result = getSingleDevice(url);
//        输出日志测试getSingleDevice的数据
        logger.info(result);
        System.out.println("程序运行完了");
        String a = "程序运行完了";
    }



    public static String getSingleDevice(String url){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet(url);
//            设置一个请求头
            httpget.setHeader("api-key", "mnJDvPM=MIiy8dmXI719R6Ixuww=");
            //Create a custom response h|andler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
//                    测试获取response里面的reasonPhrase的值
                    String a = String.valueOf(response.getEntity().getContentType());
                    if(status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status:" + status);
                    }
                }
            };
            return httpclient.execute(httpget, responseHandler);

        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }



}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值