百度鹰眼轨迹上传/驾驶行为分析

//只需要传入参数经度,维度,定位时间,实体名称即可

public static void sendpost(String jd,String wd,String loctime,String name) throws ClientProtocolException, IOException {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://yingyan.baidu.com/api/v3/track/addpoint");// 创建一个post请求
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add(new BasicNameValuePair("ak", "你的AK"));//传递的参数
        paramList.add(new BasicNameValuePair("service_id", "208484"));//传递的参数
        paramList.add(new BasicNameValuePair("entity_name", name));//传递的参数
        
        paramList.add(new BasicNameValuePair("latitude", wd));//传递的参数
        
        paramList.add(new BasicNameValuePair("speed", "130"));//传递的参数
        paramList.add(new BasicNameValuePair("direction", "2"));//传递的参数
   
         paramList.add(new BasicNameValuePair("loc_time", loctime));//传递的参数
         
        paramList.add(new BasicNameValuePair("longitude", jd));//传递的参数
        
       // paramList.add(new BasicNameValuePair("loc_time", nowTimeStamp+""));//传递的参数
        paramList.add(new BasicNameValuePair("coord_type_input", "wgs84"));//传递的参数
        HttpEntity entitya = new UrlEncodedFormEntity(paramList, "utf-8");
        post.setEntity(entitya);// 把请求实体放post请求中
        HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
        HttpEntity entity = response.getEntity();// 从response中取到响实体
        
        String html = EntityUtils.toString(entity);// 把响应实体转成文本
    }

由于百度接口的原因,很多时候就出现错误,如并发没有达到20次/秒,但却报错,因此采用循环,当上传成功的再执行下一个

boolean compe=false;
            while (compe==false) {
                try {
                    System.out.println(array.length+":"+i);
                    long sj=date.getTime()/1000;
                    sendpost(array[i][0], array[i][1], sj+"", "dl"+i);
                    sj+=1;
                    sendpost(array[i][2], array[i][3], sj+"", "dl"+i);
                    compe=true;
                    
                } catch (Exception e) {
                    // TODO: handle exception
                }
                
            }

驾驶行为分析:同样存在问题,也用循环搞定

for(int i=0;i<cStrings.length;i++) {
            System.out.println(i+":"+cStrings.length);
            boolean con=false;
            while (con==false) {
                try
                {
                HttpClient client = new DefaultHttpClient();
                Date date=sdf.parse(array[0][3]);
                Date date2=sdf.parse(array[1][3]);
                String url="http://yingyan.baidu.com/api/v3/analysis/drivingbehavior?"+"ak=你的Ak&need_mapmatch=1&transport_mode=driving&service_id=208484&entity_name=dl"+cStrings[i]+"&coord_type_output=gcj02&start_time="+date.getTime()/1000+"&end_time="+date2.getTime()/1000;
                System.out.println(url);
                HttpGet post = new HttpGet(url);// 创建一个post请求
                HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
                HttpEntity entity = response.getEntity();// 从response中取到响实体
                String html = EntityUtils.toString(entity);// 把响应实体转成文本
                System.out.println(html);
                try {
                JSONObject jsStr = JSONObject.fromObject(html); //将字符串{“id”:1}
                int status=Integer.parseInt((String)jsStr.get("status").toString());
               
                if(status==3003) {
                    strNone+=i+","+"nodata";
                    con=true;
                    
                }
                else if(status==0){
                    con=true;
                JSONArray speeding=(JSONArray)jsStr.get("speeding");
                int csnum=(Integer) jsStr.get("speeding_num");
                double[][] result=new double[csnum*2][2];
                int count=0;
                for (int j = 0; j < speeding.size(); j++) {
                    JSONObject one=speeding.getJSONObject(j);
                     JSONArray speedingPt=(JSONArray)one.get("speeding_points");
                    for (int k = 0; k < speedingPt.size(); k++) {
                        JSONObject pt=speedingPt.getJSONObject(k);
                        result[count][0]=(Double) pt.get("longitude");
                        result[count][1]=(Double) pt.get("latitude");
                        count+=1;
                        System.out.println(cStrings[i]+":"+pt.get("limit_speed"));
                        strRes+=  cStrings[i]+","+pt.get("longitude")+","+ pt.get("latitude")+","+pt.get("limit_speed")+","+pt.get("actual_speed")+"\r\n";
                        
                    }
                    
                }
                }
                }
                catch(Exception e) {
                    
                }
                }
                 catch(Exception e) {
                        
                    }
                //System.out.println(result);   
            }

python版本:

# encoding: utf-8
import json
import requests
import urllib2
def posthttp_tracks(ak, service_id, point_list):
    url = 'http://yingyan.baidu.com/api/v3/track/addpoints'
    textmod = {"ak": ak, "service_id": service_id, "point_list": point_list}
    res = requests.post(url,textmod)
    print(res._content)
point_list = [
    {
        "entity_name": '2',
        "loc_time": 1525881612,
        "latitude": 30.564468,
        "longitude": 114.31192,
        "coord_type_input": "wgs84"
   }
 ]
posthttp_tracks("CtRGuOr8Gz4puEk4hB5T35NLGahUQBHq", 208488, point_list = point_list)

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值