概述
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;
}
}
最后
以上就是犹豫自行车为你收集整理的get方式从OneNET平台查询设备信息的全部内容,希望文章能够帮你解决get方式从OneNET平台查询设备信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复