Location coordinates through addresses
Casual writing for fun
Public class getXYByAliyunAPI {public static void main (String [] args) {Connection conn = null; FileOutputStream FD = null; try {conn = JDBCUtil. getConnection (); PreparedStatement pstm = conn. prepareStatement ("select unitCode, REPLACE (unitAddress, '#','') from useWaterUnit "); ResultSet rs limit p0000.exe cuteQuery (); while (rs. next () {String unitAddress = rs. getString (2); String unitCode = rs. getString (1) + ","; String string = sendGet ("http://gc.ditu.aliyun.com/geocoding", "a =" + unitAddress + "& c = Chengdu"); string = string. replace ("}", ""); string = string. replace ("{", ""); string = string. replace ("\" "," "); string = string. replace (":", ""); String [] strings = string. split (","); for (String s: strings) {s = s. trim (); if (s. indexOf ("lon ")! =-1) {unitCode + = s. substring (s. indexOf ("lon") + 3, s. length () + ","; System. out. println (unitCode + s. substring (s. indexOf ("lon") + 3, s. length ();} if (s. indexOf ("lat ")! =-1) {unitCode + = s. substring (s. indexOf ("lat") + 3, s. length () + ", \ r \ n"; System. out. println (unitCode + s. substring (s. indexOf ("lat") + 3, s. length () ;}} fiis = new FileOutputStream ("d:/001.txt", true); fiis. write (unitCode. getBytes (); FCM. flush () ;}} catch (SQLException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackT Race ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (null! = Conn) {conn. close ();} if (null! = Fiis) {fiis. close () ;}} catch (SQLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} public static String sendGet (String url, String param) {String result = ""; BufferedReader in = null; try {String urlNameString = url + "? "+ Param; URL realUrl = new URL (urlNameString); // enable the URL connection URLConnection connection = realUrl. openConnection (); // set the common request attribute connection. setRequestProperty ("user-agent", "directclient"); // create an actual connection. connect (); // obtain all response header fields Map <String, List <String> map = connection. getHeaderFields (); // traverses all response header fields for (String key: map. keySet () {// System. out. println (key + "--->" + map. get (key ));} // Define the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (connection. getInputStream (); String line; while (line = in. readLine ())! = Null) {result + = line;} catch (Exception e) {System. out. println ("an Exception occurred when sending a GET request! "+ E); e. printStackTrace () ;}// use the finally block to close the input stream finally {try {if (in! = Null) {in. close () ;}catch (Exception e2) {e2.printStackTrace () ;}} return result ;}}