在我的应用程序中,我需要获取不同货币的当前汇率。正如这个,这个和这个问题所建议的那样,一个好的方法是使用Yahoo财经服务。
所以,当我想找到美元和加拿大元之间的汇率时,我只需发送此链接:
经过研究,我发现如果找不到汇率,则可能会出现此响应。但是这种响应是适用于我在Android 4.0下尝试的任何货币(无论是Galaxy SII还是模拟器)。因此,我无法在Android 4.0上获取汇率,但是我可以在Android 2.x上获取。
有人遇到过同样的问题吗?有没有解决方法?
编辑:这是处理从Yahoo货币服务下载汇率的线程代码。
这是我从Yahoo网站下载quotes.csv文件的功能代码:
所以,当我想找到美元和加拿大元之间的汇率时,我只需发送此链接:
http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv
这对于我的Android 2.3.4版本的Motorola Atrix手机和带有Google API 2.3.3的模拟器都可以正常工作。但是,当我尝试从装有Android ICS 4.0的Galaxy SII和带有Google API 4.0的模拟器中使用完全相同的链接时,在这两种情况下,quotes.csv文件仅包含“缺失符号列表”。经过研究,我发现如果找不到汇率,则可能会出现此响应。但是这种响应是适用于我在Android 4.0下尝试的任何货币(无论是Galaxy SII还是模拟器)。因此,我无法在Android 4.0上获取汇率,但是我可以在Android 2.x上获取。
有人遇到过同样的问题吗?有没有解决方法?
编辑:这是处理从Yahoo货币服务下载汇率的线程代码。
//show the progress dialog
downloadingDialog.show();
Runnable getRates = new Runnable() {
public void run(){
dataNotFound = false;
final String baseDir = getApplicationContext().getFilesDir().getAbsolutePath();
//download the rates from yahoo to a CSV file
downloadFileViaHTTP (baseDir);
//read the file line
String filePath = baseDir + "/" + "quotes.csv";
Log.d(tag, "-> filePath = " + filePath);
try {
// open the file for reading
InputStream instream = new FileInputStream(filePath);
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
//read the line
String fileLine = buffreader.readLine();
Log.d(tag, "fileLine = " + fileLine);
instream.close();
}
}
catch (Exception ex) {
// print stack trace.
}
}
};
final Thread t = new Thread(getRates);
t.start();
这是我从Yahoo网站下载quotes.csv文件的功能代码:
public void downloadFileViaHTTP (String localPath) {
Log.d(tag, "downloadFileViaHTTP...");
try {
//this is the Yahoo url
String urlFile = "http://download.finance.yahoo.com/d/quotes.csv?s=" + fromCurrency + toCurrency + "=X&f=sl1d1t1ba&e=.csv";
Log.d(tag,"urlFile = " + urlFile);
URL url = new URL(urlFile);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
//pointer to the downloaded file path
String localFileName = localPath + "/" + "quotes.csv";
//this is the actual downloaded file
File MyFilePtrDest = new File(localFileName);
Log.d(tag,"localFileName = " + localFileName);
//this will be used in reading the data from the Internet
InputStream inputStream = urlConnection.getInputStream();
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(MyFilePtrDest);
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//write buffer contents to file
while ((bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
}
inputStream.close();
//close the output stream when done
fileOutput.flush();
fileOutput.close();
urlConnection.disconnect();
}
catch (IOException e) {
//data were not found
dataNotFound = true;
// TODO Auto-generated catch block
e.printStackTrace();
}
}
以下是来自Google API 2.3.3模拟器的日志:
12-18 11:04:24.091: D/CurrencyConverter(414): downloadFileViaHTTP...
12-18 11:04:24.091: D/CurrencyConverter(414): urlFile = http://download.finance.yahoo.com/d/quotes.csv?s=EURUSD=X&f=sl1d1t1ba&e=.csv
12-18 11:04:24.282: D/CurrencyConverter(414): localFileName = /data/data/com.myapps.currencyconverter/files/quotes.csv
12-18 11:04:24.461: D/CurrencyConverter(414): -> filePath = /data/data/com.myapps.currencyconverter/files/quotes.csv
12-18 11:04:24.461: D/CurrencyConverter(414): fileLine = "EURUSD=X",1.3172,"12/18/2012","4:03am",1.317,1.3174
这是来自Google API 4.0模拟器的日志:
12-18 11:47:36.130: D/CurrencyConverter(668): downloadFileViaHTTP...
12-18 11:47:36.130: D/CurrencyConverter(668): urlFile = http://download.finance.yahoo.com/d/quotes.csv?s=EURUSD=X&f=sl1d1t1ba&e=.csv
12-18 11:47:36.449: D/dalvikvm(668): GC_CONCURRENT freed 306K, 4% free 11714K/12167K, paused 5ms+10ms
12-18 11:47:36.951: D/CurrencyConverter(668): localFileName = /data/data/com.myapps.currencyconverter/files/quotes.csv
12-18 11:47:37.159: D/CurrencyConverter(668): -> filePath = /data/data/com.myapps.currencyconverter/files/quotes.csv
12-18 11:47:37.159: D/CurrencyConverter(668): fileLine = Missing Symbols List.
从第一个例子中可以看到,"fileLine"字符串变量获取了正确的汇率,而在第二个例子中,quotes.csv文件仅包含"Missing Symbols List."的值。
编辑2:我已经上传了一个共享文件夹,里面包含完整的Android项目,大家都可以尝试。您可以在Android 2.x和4模拟器(或手机如果您有的话)上编译和运行。
编辑3:虽然这不是一个直接的答案,但仍然是一个解决方法。我使用Google货币计算器代替Yahoo货币计算器。要使用Google货币计算器,只需将Yahoo网址更改为此网址:http://www.google.com/ig/calculator?hl=en&q=1" + fromCurrency + "=?" + toCurrency。点击此链接以查看将78欧元转换为美元的示例。我检查过,它适用于Android的两个版本。但是,如果有人发现为什么Yahoo网站会出现这种情况,与我们分享一下就好了。