fork download
  1. package googleMap;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.net.URLEncoder;
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. import org.dom4j.Document;
  13. import org.dom4j.DocumentException;
  14. import org.dom4j.Element;
  15. import org.dom4j.io.SAXReader;
  16. import org.dom4j.io.XMLWriter;
  17.  
  18.  
  19. public class queryDistance {
  20.  
  21. static final String GET_URL = "https://m...content-available-to-author-only...s.com/maps/api/distancematrix/xml?";
  22.  
  23. public void query(String str1, String str2) throws IOException, DocumentException {
  24. // TODO 自動產生的方法 Stub
  25. // 拼湊get請求的URL字串,使用URLEncoder.encode對特殊和不可見字符進行編碼
  26. String origin_location = URLEncoder.encode(str1, "utf-8");
  27. String des_location = URLEncoder.encode(str2, "utf-8");
  28. String getURL = GET_URL + "origins=" + origin_location + "&destinations=" + des_location + "&language=zh_TW&sensor=false";
  29.  
  30. URL getUrl = new URL(getURL);
  31. // 根據拼湊的URL,打開連接,URL.openConnection函數會根據URL的類型,
  32. // 返回不同的URLConnection子類的對象,這裏URL是一個http,因此實際返回的是HttpURLConnection
  33. HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
  34. // 進行連接,但是實際上get request要在下一句的connection.getInputStream()函數中才會真正發到
  35. // 服務器
  36. connection.connect();
  37. // 取得輸入流,並使用Reader讀取
  38.  
  39. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//設置編碼,否則中文亂碼
  40. System.out.println("=============================");
  41. System.out.println("Contents of get request");
  42. System.out.println("=============================");
  43. String lines;
  44. while ((lines = reader.readLine()) != null){
  45. //lines = new String(lines.getBytes(), "utf-8");
  46. System.out.println(lines);
  47. }
  48. reader.close();
  49. // 斷開連接
  50. connection.disconnect();
  51. System.out.println("=============================");
  52. System.out.println("Contents of get request ends");
  53. System.out.println("=============================");
  54.  
  55. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:55: error: reached end of file while parsing
	}
	 ^
1 error
stdout
Standard output is empty