Advertisement

javaweb百度鹰眼上传轨迹接口

阅读量:

百度鹰眼服务系统:访问该服务系统的途径链接至:http://lbsyun.baidu.com/index.php?title=yingyan/api/v3/trackupload

可以上传单个轨迹和多个轨迹

上传单个轨迹实例:

复制代码
 public static void main(String[] args) {

    
 		String param = "";
    
 		String url = "http://yingyan.baidu.com/api/v3/track/addpoint";
    
 		String ak = "ak";
    
 		int service_id = 000000;//注意这里是服务的id不是应用的id
    
 		String entity_name = "name";
    
 		double latitude = 30;
    
 		double longitude = 20;
    
 		long loc_time = System.currentTimeMillis() / 1000;
    
 		String coord_type_input = "bd09ll";
    
 		param = "ak=" + ak + "&" + "service_id=" + service_id + "&"
    
 				+ "entity_name=" + entity_name + "&" + "" + "latitude="
    
 				+ latitude + "&" + "longitude=" + longitude + "&" + "loc_time="
    
 				+ loc_time + "&" + "coord_type_input=" + coord_type_input;
    
 		String str = sendPost(url, param);
    
 		System.out.println(str);
    
 	}

我的测试代码因此将其放置在main方法中。sendPost是一个源自百度查询到的JAVA后台请求URL的方法。具体实现见下文。

复制代码
 public static String sendPost(String url, String param) {

    
 		PrintWriter out = null;
    
 		BufferedReader in = null;
    
 		String result = "";
    
 		try {
    
 			URL realUrl = new URL(url);
    
 			// 打开和URL之间的连接
    
 			URLConnection conn = realUrl.openConnection();
    
 			// 设置通用的请求属性
    
 			conn.setRequestProperty("accept", "*/*");
    
 			conn.setRequestProperty("connection", "Keep-Alive");
    
 			conn.setRequestProperty("user-agent",
    
 					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    
 			// 发送POST请求必须设置如下两行
    
 			conn.setDoOutput(true);
    
 			conn.setDoInput(true);
    
 			// 获取URLConnection对象对应的输出流
    
 			out = new PrintWriter(conn.getOutputStream());
    
 			// 发送请求参数
    
 			out.print(param);
    
 			// flush输出流的缓冲
    
 			out.flush();
    
 			// 定义BufferedReader输入流来读取URL的响应
    
 			in = new BufferedReader(
    
 					new InputStreamReader(conn.getInputStream()));
    
 			String line;
    
 			while ((line = in.readLine()) != null) {
    
 				result += line;
    
 			}
    
 		} catch (Exception e) {
    
 			System.out.println("发送 POST 请求出现异常!" + e);
    
 			e.printStackTrace();
    
 		}
    
 		// 使用finally块来关闭输出流、输入流
    
 		finally {
    
 			try {
    
 				if (out != null) {
    
 					out.close();
    
 				}
    
 				if (in != null) {
    
 					in.close();
    
 				}
    
 			} catch (IOException ex) {
    
 				ex.printStackTrace();
    
 			}
    
 		}
    
 		return result;
    
 	}

全部评论 (0)

还没有任何评论哟~