5

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

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

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

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

//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();

这是我从雅虎网站下载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:我已经将完整的Android项目上传到一个共享文件夹,所以大家可以试试。您可以使用 Android 2.x 和 4 模拟器(或手机,如果您有 :-))编译和运行

EDIT3:虽然这不是一个直接的答案,但它仍然是一种解决方法。我使用谷歌货币计算器而不是雅虎。要使用 Google 货币计算器,只需将 Yahoo 网址更改为以下网址:http ://www.google.com/ig/calculator?hl=en&q=1 " + fromCurrency + "=?" + toCurrency。点击此链接到看一个将 78 欧元兑换成美元的例子。我检查了一下,它适用于两个 Android 版本。但是,如果有人发现雅虎网站发生这种情况的原因,最好与我们分享。

4

1 回答 1

1

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

这里这里报告了这个问题

于 2012-12-29T18:20:07.440 回答