gpt4 book ai didi

android - Android 4 中的雅虎货币汇率

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:10 25 4
gpt4 key购买 nike

在我的应用程序中,我需要获取不同货币的当前汇率。正如 this 所建议的那样, thisthis一个很好的方法是使用雅虎财经服务。

因此,例如,当我想查找美元和加元之间的汇率时,我只需发送此链接: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。

有没有人遇到过同样的问题?有什么解决方法吗?

编辑:这是处理雅虎货币服务下载率的线程代码:

//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 文件仅包含“缺失符号列表”。值(value)。

EDIT2:我已经上传到 shared folder完整的Android工程,大家可以试试。您可以使用 Android 2.x 和 4 模拟器(或手机,如果您有:-))编译和运行

EDIT3:虽然这不是直接的答案,但它仍然是一种解决方法。我使用 Google 货币计算器而不是 Yahoo 计算器。要使用 Google 货币计算器,只需将 Yahoo 网址更改为:http://www.google.com/ig/calculator?hl=en&q=1。 "+ fromCurrency + "=?"+ toCurrency。单击 this link 查看将 78 欧元转换为美元的示例。我检查过它适用于两个 Android 版本。但是如果有人发现为什么 Yahoo 网站会发生这种情况,与我们分享它会很好..

最佳答案

在 ICS 中运行时尝试删除 urlConnection.setDoOutput(true);。因为 ICS 在 setDoOutput(true) 时将 GET 请求转为 POST。

报告此问题herehere

关于android - Android 4 中的雅虎货币汇率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com