Location coordinates through addresses

Source: Internet
Author: User

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 ;}}

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.