从美元获得20个其他汇率的货币兑换率。

时间:2014-05-14 17:06:16

标签: java url bufferedreader bloomberg

此代码采用from和to参数,我需要从USD兑换20个其他费率。有没有我可以给它一个字符串数组(转换为),而不是它需要连接到网站20次,每次加载价格需要10-15秒。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package priceStrategy;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author 
 */
public class ExchangeFinder {

    /**
     * The method takes 2 currency strings and return the exchange rate.
     *
     * @param fromCurrency String the currency to exchange from
     * @param toCurrency String the currency to exchange to
     * @return double the exchange rate between fromCurrency and toCurrency. If
     * something goes wrong 100.0 will be returned.
     *
     * USD - DKK USD - JPY USD - GBP USD - AUD USD - EUR USD - ESP USD - GHS USD
     * - ILS USD - KES USD - JOD USD - LKR USD - LVL USD - MAD USD - MWK USD -
     * NOK USD - PHP USD - NOK USD - PKR USD - RUB USD - SGD
     */
    public static double getExchangeRate(String fromCurrency, String toCurrency) {
        double result = 100.0;

        try {
            // Open a connection to bloomberg to get exchange rates  
            URL bloombergCurrency = new URL("http://www.bloomberg.com/quote/" + fromCurrency + toCurrency + ":CUR");
            URLConnection bc = bloombergCurrency.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(bc.getInputStream()));

            String inputLine;  //Used to read in lines from webpage
            boolean found = false;  //Flag set true if the exchange rate is found in all the lines            
            // 1) read in line and if it's not null and the default result has not been changed...
            while ((inputLine = in.readLine()) != null && result == 100.0) {
                if (found) {  //..2) if found == true then we have got the correct exchange rate
                    result = Double.parseDouble(inputLine);
                }
                //..3) looking for the exchange rate in the lines. It's right after this string
                if (inputLine.trim().equals("<span class=\" price\">")) {
                    found = true;
                }
            }
            in.close(); //DONE. Closing connection.          

            if (!found) {
                System.out.println("Error: Never found the currency you asked for!");
            }  //Message if currency not found
        } catch (MalformedURLException ex) {
            System.out.println("MalformedURLException in getExchangeRate(): Invalid URL.");
        } catch (NumberFormatException ex) {
            System.out.println("NumberFormatException in getExchangeRate(): Invalid response from server.");
        } catch (IOException ex) {
            System.out.println("IOException in getExchangeRate(): Cannot connect to server.");
        }

        return result;
    }

}

1 个答案:

答案 0 :(得分:1)

我绝对不会为了你需要做的每一次转换而反复打电话给别人的网站。这不仅会为他们带来不必要的流量,实际上可能违反他们的使用政策(我还没有检查过您的具体情况),他们可能会随机阻止您的IP访问他们的服务,这会破坏您的应用程序。

最好的方法(如果您允许以这种方式使用他们的服务)将构建您自己的数据库&#34; (通过每个目标货币一次调用bloomberg来获得汇率并记住它)的汇率(20个数字的大字)汇率。然后,使用数据库中的数据自行完成所需的转换。您需要决定的唯一事情是通过再次为每种货币调用bloomberg来更新数据库的频率(每天一次?每周一次?)。但是我会小心不要过分...... :)

这种方法不仅在构建数据库后运行得更快,而且如果(您的链接)bloomberg发生故障或者阻止您或更改其网站界面,它也会继续工作,在这种情况下您的应用程序将只是使用最后已知的汇率。你甚至可以故障转移到另一个&#34;提供商&#34;并在那里获得汇率。

一边评论:如果出现问题,我不会返回100.0,因为如果一切正常,这可能实际上是一个有效的结果,可以返回一个转换。使用负数或0或其他一些不可能的汇率。