Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将货币汇率从美元兑换成其他20种汇率。_Java_Url_Bufferedreader_Bloomberg - Fatal编程技术网

Java 将货币汇率从美元兑换成其他20种汇率。

Java 将货币汇率从美元兑换成其他20种汇率。,java,url,bufferedreader,bloomberg,Java,Url,Bufferedreader,Bloomberg,这段代码需要一个from和to参数,我需要将美元兑换成其他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 i

这段代码需要一个from和to参数,我需要将美元兑换成其他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;
    }

}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
一揽子价格策略;
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.URLConnection;
/**
*
*@作者
*/
公共类交换文件{
/**
*该方法采用2个货币字符串并返回汇率。
*
*@param fromCurrency字符串要从中兑换的货币
*@param toCurrency字符串要兑换的货币
*@return两倍于从货币到货币之间的汇率。如果
*出现问题时,将返回100.0。
*
*美元-丹麦克朗美元-日元美元-英镑美元-澳元美元-欧元美元-ESP美元-GHS美元
*-ILS美元-KES美元-JOD美元-LKR美元-LVL美元-MAD美元-MWK美元-
*挪威克朗美元-菲律宾比索美元-挪威克朗美元-菲律宾卢比美元-新加坡元
*/
公共静态双getExchangeRate(字符串从货币,字符串到货币){
双结果=100.0;
试一试{
//打开与彭博社的连接以获取汇率
URL bloombergCurrency=新URL(“http://www.bloomberg.com/quote/“+从货币+到货币+”:CUR”);
URLConnection bc=bloombergCurrency.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(bc.getInputStream());
字符串inputLine;//用于从网页中读入行
boolean found=false;//如果在所有行中都找到了汇率,则标志设置为true
//1)读取第行,如果该行不为空且默认结果未更改。。。
while((inputLine=in.readLine())!=null&&result==100.0){
如果(find){/..2)如果find==true,那么我们就得到了正确的汇率
结果=Double.parseDouble(inputLine);
}
//…3)在行中查找汇率。它就在这个字符串后面
if(inputLine.trim()等于(“”){
发现=真;
}
}
in.close();//完成。正在关闭连接。
如果(!找到){
System.out.println(“错误:从未找到您要的货币!”);
}//如果找不到货币,则显示消息
}捕获(格式错误){
System.out.println(“getExchangeRate()中的MalformedURLException:无效URL”);
}捕获(NumberFormatException ex){
System.out.println(“getExchangeRate()中的NumberFormatException:来自服务器的响应无效”);
}捕获(IOEX异常){
System.out.println(“getExchangeRate()中的IOException:无法连接到服务器。”);
}
返回结果;
}
}

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

最好的方法(如果你被允许以这种方式使用他们的服务)是建立你自己的汇率“数据库”(20个数字的大字),只需按目标货币给彭博社打一次电话,获取汇率并记住它。然后,使用数据库中的数据自己进行所需的转换。你需要决定的唯一一件事是每种货币(每天一次?每周一次?)通过再次致电彭博社更新数据库的频率。但我会小心不要做得太过分……:)

这种方法不仅在你建立了数据库之后运行得更快,而且在彭博社(你的链接)宕机或他们阻止你或更改他们的网站界面时,它也能继续工作,在这种情况下,你的应用程序将只使用上一次已知的汇率。您甚至可以将故障转移到另一个“提供商”并将汇率转移到那里


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

“使用负数或0”=>或者更好地抛出异常!加快流程的最简单方法是通过将任务提交给ExecutorService来并行发送20个查询。