在我的申请中,我需要获得不同货币的当前汇率。正如this所建议的,this和this提出了一个很好的方法,就是使用Yahoo Finance服务。
所以,当我想找到美元和加元之间的比率时,我只需发送此链接:http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv
这适用于我的带有Android 2.3.4的摩托罗拉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 Currency Service下载费率的线程代码:
//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文件仅包含“缺少符号列表”。值。
EDIT2:我已经上传到shared folder完整的Android项目,所以每个人都可以尝试一下。您可以使用Android 2.x和4个仿真器编译和运行(如果您有以下情况,则可以运行电话: - ))
EDIT3:虽然这不是一个直接的答案,但仍然是一种解决方法。我使用谷歌货币计算器而不是雅虎货币计算器。要使用Google货币计算器,只需更改雅虎网址:http://www.google.com/ig/calculator?hl=en&q=1“+ fromCurrency +”=?“+ toCurrency。点击this link查看将78欧元转换为美元的示例。检查,它适用于两个Android版本。但是,如果有人发现为什么这发生在雅虎网站上,最好与我们分享..