2013-10-23 93 views

回答

0

對於www.webservicex.net中的某些服務,您可以使用HTTP執行GET請求。例如:

protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 

    String from = "USD"; 
    String to = "EUR"; 

    java.net.URL url = new java.net.URL(
      "http://www.webservicex.net/CurrencyConvertor.asmx" 
        + "/ConversionRate?FromCurrency=" + from 
        + "&ToCurrency=" + to); 
    java.util.Scanner sc = new java.util.Scanner(url.openStream()); 

    // <?xml version="1.0" encoding="utf-8"?> 
    sc.nextLine(); 

    // <double xmlns="http://www.webserviceX.NET/">0.724</double> 
    String str = sc.nextLine().replaceAll("^.*>(.*)<.*$", "$1"); 

    sc.close(); 

    Double rate = Double.parseDouble(str); 
    log("Rate: " + rate); 

} 
+0

hm ....我在Eclipse中使用了「Web服務」選項,它下載了項目中的所有類。但這樣看起來更容易。謝謝! –