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.  
  20.  
  21.  
  22. public class queryDistance {
  23.  
  24. static final String GET_URL = "https://m...content-available-to-author-only...s.com/maps/api/distancematrix/xml?";
  25.  
  26. public void query(String str1, String str2) throws IOException, DocumentException {
  27. // TODO 自動產生的方法 Stub
  28. // 拼湊get請求的URL字串,使用URLEncoder.encode對特殊和不可見字符進行編碼
  29. String origin_location = URLEncoder.encode(str1, "utf-8");
  30. String des_location = URLEncoder.encode(str2, "utf-8");
  31. String getURL = GET_URL + "origins=" + origin_location + "&destinations=" + des_location + "&language=zh_TW&sensor=false";
  32.  
  33. URL getUrl = new URL(getURL);
  34. // 根據拼湊的URL,打開連接,URL.openConnection函數會根據URL的類型,
  35. // 返回不同的URLConnection子類的對象,這裏URL是一個http,因此實際返回的是HttpURLConnection
  36. HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
  37. // 進行連接,但是實際上get request要在下一句的connection.getInputStream()函數中才會真正發到
  38. // 服務器
  39. connection.connect();
  40. // 取得輸入流,並使用Reader讀取
  41.  
  42. BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
  43. XMLWriter writer = new XMLWriter( new FileWriter("F:\\gmap.xml"));
  44. String lines;
  45. while ((lines = rd.readLine()) != null){
  46. writer.write(lines);
  47. writer.write("\n");
  48. }
  49. writer.close();
  50.  
  51. File f = new File("F:\\gmap.xml");
  52. SAXReader reader = new SAXReader();
  53. Document doc = reader.read(f);
  54. Element root = doc.getRootElement();
  55. Element foo;
  56. for (Iterator i = root.elementIterator("distance"); i.hasNext();) {
  57. foo = (Element) i.next();
  58. System.out.print("車牌號碼:" + foo.elementText("value"));
  59. }
  60.  
  61. }
  62. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:22: error: class queryDistance is public, should be declared in a file named queryDistance.java
public class queryDistance {
       ^
Main.java:12: error: package org.dom4j does not exist
import org.dom4j.Document;
                ^
Main.java:13: error: package org.dom4j does not exist
import org.dom4j.DocumentException;
                ^
Main.java:14: error: package org.dom4j does not exist
import org.dom4j.Element;
                ^
Main.java:15: error: package org.dom4j.io does not exist
import org.dom4j.io.SAXReader;
                   ^
Main.java:16: error: package org.dom4j.io does not exist
import org.dom4j.io.XMLWriter;
                   ^
Main.java:26: error: cannot find symbol
	public void query(String str1, String str2) throws IOException, DocumentException {
	                                                                ^
  symbol:   class DocumentException
  location: class queryDistance
Main.java:43: error: cannot find symbol
		XMLWriter writer = new XMLWriter( new FileWriter("F:\\gmap.xml"));
		^
  symbol:   class XMLWriter
  location: class queryDistance
Main.java:43: error: cannot find symbol
		XMLWriter writer = new XMLWriter( new FileWriter("F:\\gmap.xml"));
		                       ^
  symbol:   class XMLWriter
  location: class queryDistance
Main.java:52: error: cannot find symbol
		SAXReader reader = new SAXReader();
		^
  symbol:   class SAXReader
  location: class queryDistance
Main.java:52: error: cannot find symbol
		SAXReader reader = new SAXReader();
		                       ^
  symbol:   class SAXReader
  location: class queryDistance
Main.java:53: error: cannot find symbol
		Document doc = reader.read(f);
		^
  symbol:   class Document
  location: class queryDistance
Main.java:54: error: cannot find symbol
		Element root = doc.getRootElement();
		^
  symbol:   class Element
  location: class queryDistance
Main.java:55: error: cannot find symbol
		Element foo;
		^
  symbol:   class Element
  location: class queryDistance
Main.java:57: error: cannot find symbol
			foo = (Element) i.next();
			       ^
  symbol:   class Element
  location: class queryDistance
15 errors
stdout
Standard output is empty