Packagecom.action;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.util.ArrayList;Importjava.util.List;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject; Public classTest {/*** @Author Liuchao email:[email protected] * @data 2017-5-11 *@version1.0.1 *@paramargs*/ Public Staticstring Get (String urlall,string charset) {BufferedReader reader=NULL; String result=NULL; StringBuffer SBF=NewStringBuffer (); String useragent= "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/29.0.1547.66 safari/537.36";//Simulation Browser Try{URL URL=NewURL (Urlall); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setrequestmethod ("GET"); Connection.setreadtimeout (30000); Connection.setconnecttimeout (30000); Connection.setrequestproperty ("User-agent", useragent); Connection.connect (); InputStream is=Connection.getinputstream (); Reader=NewBufferedReader (NewInputStreamReader (IS, CharSet)); String Strread=NULL; while((Strread = Reader.readline ())! =NULL) {sbf.append (strread); Sbf.append ("\ r"); } reader.close (); Result=sbf.tostring (); } Catch(Exception e) {e.printstacktrace (); } returnresult; } Public StaticList<object>Jeixi (String msg) {List<Object> list=NewArraylist<object>(); String JSON=msg; //jsonparser parser=new jsonparser ();Jsonobject obj=jsonobject.fromobject (msg); Jsonarray result= (Jsonarray) obj.get ("Result"); Jsonobject RESULT1= (jsonobject) result.get (0); Jsonobject Location= (jsonobject) result1.get ("Location"); List.add (Obj.get ("Status")); List.add (Result1.get ("City")); List.add (Result1.get ("District")); List.add (Result1.get ("Name")); List.add (Location.get ("LNG")); List.add (Location.get ("Lat")); List.add (Obj.get ("Message")); List.add (Result1.get ("UID")); List.add (Result1.get ("Businesss")); List.add (Result1.get ("Cityid")); List.add (result); returnlist; } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//String url= "http://api.map.baidu.com/place/v2/suggestion?query=Guang Bu Tun ®ion= Hubei province &city_limit=true&output=json&ak=grhqooqpoqzvm8nmrnvoqswejvggglqy, Hongshan District, Wuhan City, Hubei provinceString url2= "http://api.map.baidu.com/place/v2/suggestion?query=, Pudong New Area, Shanghai Shenjiang ®ion= Shanghai &city_limit=true &output=json&ak=grhqooqpoqzvm8nmrnvoqswejvggglqy "; String result=test.get (Url2, "UTF-8"); List<Object> result1=Test.jeixi (Result); for(intI=0;i<result1.size (); i++) {System.out.println (Result1.get (i)); } }}
Java gets coordinates by transmitting address