本文主要介绍对接百度鹰眼工具,包含创建entity、查询entity、轨迹上传、创建围栏等。需要的朋友可以参考一下。
1.YinyanUtil.java
public class YinyanUtil {
/**
* 百度鹰眼服务器端Key
*/
public static final String BAIDU_YINGYAN_AK_S = "百度鹰眼服务器端Key";
/**
* 百度鹰眼服务ID
*/
public static final String BAIDU_YINGYAN_SERVICE_ID = "百度鹰眼服务ID";
/**
* 百度鹰眼接口URL
*/
public static final String BAIDU_YINGYAN_URL = "http://yingyan.baidu.com/api/v3/";
/**
* 百度鹰眼请求方式(POST)
*/
public static final String BAIDU_YINGYAN_REQ_POST = "POST";
/**
* 百度鹰眼请求方式(GET)
*/
public static final String BAIDU_YINGYAN_REQ_GET = "GET";
/**
* 逆地理编码
*/
public static final String BAIDU_REVERSE_GENCODING = "http://api.map.baidu.com/reverse_geocoding/v3/";
public static String operEntityReq(String urlReq, String param, String method) {
try {
//Get请求,拼装参数
if (BAIDU_YINGYAN_REQ_GET.equals(method)) {
urlReq = urlReq + "?" + param;
}
//创建URL对象
URL url = new URL(urlReq);
//返回一个URLConnection对象,它表示到URL所引用的远程对象的连接
URLConnection urlConnection = url.openConnection();
//POST请求,写入参数
if (BAIDU_YINGYAN_REQ_POST.equals(method)) {
// 设置doOutput属性为true表示将使用此urlConnection写入数据
urlConnection.setDoOutput(true);
// 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 得到请求的输出流对象
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
// 把数据写入请求的Body
out.write(param);
out.flush();
out.close();
}
// 从服务器读取响应
InputStream inputStream = urlConnection.getInputStream();
String encoding = urlConnection.getContentEncoding();
String result = IOUtils.toString(inputStream, encoding);
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 创建entity
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String createEntity(String urlPreFix,String ak,String serviceId,String entityName){
String urlReq = urlPreFix + YingyanEntity.ADD;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&entity_name=" + entityName +
"&entity_desc=" + entityName;
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
return result;
}
/**
* 查询entity
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String queryEntity(String urlPreFix,String ak,String serviceId,String entityName){
String urlReq = urlPreFix + YingyanEntity.LIST;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&filter=entity_names:" + entityName +"";
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_GET);
return result;
}
/**
* 更新entity
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String updateEntity(String urlPreFix,String ak,String serviceId,String entityName){
String urlReq = urlPreFix + YingyanEntity.UPDATE;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&entity_name=" + entityName +
"&entity_desc=" + entityName;
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
return result;
}
/**
* 删除entity
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String delEntity(String urlPreFix,String ak,String serviceId,String entityName){
String urlReq = urlPreFix + YingyanEntity.DELETE;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&entity_name=" + entityName;
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
return result;
}
/**
* 轨迹上传
* @param urlPreFix
* @param list
*/
public static String uploadLocus(String urlPreFix, List list){
String result = "";
try {
//创建一个HTTPClient
String url = urlPreFix + YingyanEntity.ADDPOINTS;
HttpClientUtil ct = new HttpClientUtil(url, list);
//默认方式创建
ct.setClient(HttpClients.createDefault());// 创建默认http连接
//开始请求
result = ct.startHttp(ct.getClient());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(result);
return result;
}
/**
* 创建围栏
* @param urlPreFix
* @param list
*/
public static String createFence(String urlPreFix,List list){
String result = "";
try {
//创建一个HTTPClient
String url = urlPreFix + YingyanEntity.CIRCLEFENCE;
HttpClientUtil ct = new HttpClientUtil(url, list);
//默认方式创建
ct.setClient(HttpClients.createDefault());// 创建默认http连接
//开始请求
result = ct.startHttp(ct.getClient());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(result);
return result;
}
/**
* 查询围栏
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String queryFence(String urlPreFix,String ak,String serviceId,String entityName){
String urlReq = urlPreFix + YingyanEntity.FENCEQUERYSTATUS;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&monitored_person=" + entityName;
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_GET);
return result;
}
/**
* 围栏列表
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String queryFenceList(String urlPreFix,String ak,String serviceId,String entityName,Integer page_index,Integer page_size){
String urlReq = urlPreFix + YingyanEntity.FENCEQUERYLIST;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&monitored_person=" + entityName;
if(page_index != null){
param = param + "&page_index=" + page_index;
}
if(page_size != null){
param = param + "&page_size=" + page_size;
}
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_GET);
return result;
}
/**
* 删除围栏
* @param urlPreFix
* @param ak
* @param serviceId
* @param fence_ids
* @return
*/
public static String delFenceList(String urlPreFix,String ak,String serviceId,String fence_ids){
String urlReq = urlPreFix + YingyanEntity.FENCEDELETELIST;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&fence_ids=" + fence_ids;
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
return result;
}
/**
* 获取用户实时轨迹
* @param urlPreFix
* @param ak
* @param serviceId
* @param entityName
* @return
*/
public static String getlatestpoint(String urlPreFix,String ak,String serviceId,String entityName){
String urlReq = urlPreFix + YingyanEntity.LATESTPOINT;
String param = "ak=" + (StringUtils.isEmpty(ak) ? BAIDU_YINGYAN_AK_S : ak) +
"&service_id=" + (StringUtils.isEmpty(serviceId) ? BAIDU_YINGYAN_SERVICE_ID : serviceId) +
"&entity_name=" + entityName;
String result = operEntityReq(urlReq, param, BAIDU_YINGYAN_REQ_GET);
return result;
}
/**
* 逆地理编码
* @param ak
* @param location
* @return
*/
public static String reversegGeocoding(String ak, String location){
String url = BAIDU_REVERSE_GENCODING + "?ak=" + ak + "&output=json&coordtype=wgs84ll" + "&location=" + location;
String result = operEntityReq(url, null, BAIDU_YINGYAN_REQ_GET);
return result;
}
public static String toJsonStr(Object object) {
if(object == null) {
return "";
}
try {
return JSONObject.toJSONString(object);
} catch(Exception e) {
}
return "";
}
}
2.YingyanEntity.java
public class YingyanEntity {
/**
* 创建entity,并赋属性信息
*/
public static final String ADD ="entity/add";
/**
* 删除entity
*/
public static final String DELETE ="entity/delete";
/**
* 更新entity属性信息
*/
public static final String UPDATE ="entity/update";
/**
* 检索符合条件的entity,返回entity属性信息和最新位置。可用于列出entity,也可用于批量查询多个entitiy的位置
*/
public static final String LIST ="entity/list";
/**
* 轨迹上传能力
*/
public static final String ADDPOINTS ="track/addpoints";
/**
* 创建圆形围栏
*/
public static final String CIRCLEFENCE ="fence/createcirclefence";
/**
* 查询围栏状态
*/
public static final String FENCEQUERYSTATUS ="fence/querystatus";
/**
* 查询实时轨迹
*/
public static final String LATESTPOINT ="track/getlatestpoint";
/**
* 围栏列表
*/
public static final String FENCEQUERYLIST ="fence/list";
/**
* 围栏删除
*/
public static final String FENCEDELETELIST ="fence/delete";
}