package com.lwz.wx.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.io.IOException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import com.lwz.wx.bean.ReLocation; import com.thoughtworks.xstream.XStream; // 根据发过来的经纬度查询附近的酒店,营业部等等 public class GetLocationMessage { //public String[] ss =new String[10]; public List<ReLocation> locs=new ArrayList<ReLocation>(); public String palceRequestUrl(String query,String lat,String lng) throws UnsupportedEncodingException { String url = "http://api.map.baidu.com/place/v2/search?"+ "query=" + URLEncoder.encode(query,"UTF-8")+ "&ak=" + "百度申请KEY"+"&location="+lat+","+lng +"&radius=1500"+"&output="+"json"; // return url; }
//发起请求获取信息 public List<ReLocation> getPalace(String query,String lat,String lng) throws Exception{String url = palceRequestUrl(query,lat,lng); URL outurl=null; String inputline=""; String info=""; outurl=new URL(url); HttpURLConnection conn=(HttpURLConnection) outurl.openConnection(); conn.setRequestMethod("GET"); InputStreamReader inReader=new InputStreamReader(conn.getInputStream(),"UTF-8"); BufferedReader bufferedReader=new BufferedReader(inReader); while((inputline=bufferedReader.readLine())!=null){ info+=inputline; } JSONObject jsonObj = JSONObject.fromObject(info); String status = jsonObj.getString("status"); String results = jsonObj.getString("results"); JSONArray jsonArray3 = JSONArray.fromObject(results);//results String[]ss=new String[jsonArray3.size()]; for(int i=0; i< jsonArray3.size();i++){ ss[i]=jsonArray3.getString(i); } for(int i=0;i<ss.length;i++){ JSONObject jsonObj4 = JSONObject.fromObject(ss[i]); if(jsonObj4.getString("address")!=null){ ReLocation loc=new ReLocation(); loc.setAddress(jsonObj4.getString("address")); loc.setName(jsonObj4.getString("name")); loc.setLocation(jsonObj4.getString("location")); locs.add(loc); } } return locs; }
public String runxx(String query,String inlat,String inlng){ GetLocationMessage test=new GetLocationMessage(); String name=""; String lat=""; String lng=""; StringBuffer buff=new StringBuffer(); try { List<ReLocation> aa= test.getPalace(query,inlat,inlng); for (ReLocation reloca : aa){ JSONObject jsonObj = JSONObject.fromObject(reloca.getLocation() ); name= reloca.getName(); String address=reloca.getAddress(); lat = jsonObj.getString("lat"); lng = jsonObj.getString("lng"); buff.append(name+"@"+lat+"@"+lng+"@"+address).append("#").append("\n"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(buff.toString()); return buff.toString(); } public static void main(String[] args) throws IOException { GetLocationMessage test=new GetLocationMessage(); System.out.println(test.runxx("酒楼", "26.113909", "119.268723")); String[] ss=test.runxx("团购", "26.110846", "119.268723").split("#"); }//主函数测试。
|
|