httpclient请求百度palceAPI

/**
 * 百度placeapi通过关键字,经纬度获取周边
 * @param modelName  
 * @param latitude
 * @param longitude
 * @return <placeAPI>list
 */
private List placeList(String modelName,float latitude,float longitude) {
    List list=new ArrayList();
    String telephone;
    try{
    String palceUrl="http://api.map.baidu.com/place/v2/search";
    String queryString="ak=tEtTWYjFFmUxyqoegemuktaA&query="+modelName+"&location="+latitude+","+longitude+"&radius=1000&output=json&scope=2&page_size=20";
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(10000);
    HttpClient client=new HttpClient(httpConnectionParams);
    HttpMethod method=new GetMethod(palceUrl);

    String encodeQuery;
    encodeQuery = URIUtil.encodeQuery(queryString,"UTF-8");
    method.setQueryString(encodeQuery);
    client.executeMethod(method);

    BufferedReader reader=null;
    reader=new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),"UTF-8"));
    String line=null;
    StringBuffer respons=new StringBuffer();
    while((line=reader.readLine())!=null){
        respons.append(line).append(System.getProperty("line.separator"));
    }
    reader.close();
    method.releaseConnection();
    String sTotalString=respons.toString();
    JSONObject json;
    json=JSONObject.fromString(sTotalString);

    JSONArray results = json.getJSONArray("results");

    for(int i=0;i<results.length();i++){
     Location_place location=new Location_place();
        PlaceAPI placeAPI=new PlaceAPI();
        Detail_info detail_info=new Detail_info();
     JSONObject result=results.getJSONObject(i);
     placeAPI.setName(result.getString("name"));
     placeAPI.setAddress(result.getString("address"));
     try{
        telephone=result.getString("telephone");
     }
     catch(Exception e){
        telephone="";
     }
     placeAPI.setTelephone(telephone);
     JSONObject  jsonlocation= (JSONObject) result.get("location");
     float lat=Float.parseFloat(jsonlocation.getString("lat"));
     float lng=Float.parseFloat(jsonlocation.getString("lng"));
     location.setLat(lat);
     location.setLng(lng);
     placeAPI.setLocation(location);
     JSONObject jsondetail_info=(JSONObject) result.get("detail_info");
     int distance=Integer.parseInt(jsondetail_info.getString("distance"));
     detail_info.setDistance(distance);
     placeAPI.setDetail_info(detail_info);
     list.add(placeAPI) ;
    }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return list;


} `这里写代码片`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值