2012-12-18 100 views
5

在我的申請中,我需要獲得不同貨幣的當前匯率。正如this,thisthis提出的一個很好的方法是使用Yahoo Finance服務。Android中的雅虎貨幣匯率4

所以,當我想找到,例如,美元和加元的速度,我只需發送此鏈接:http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv

這是工作的罰款我都摩托羅拉Atrix手機採用Android 2.3.4模擬器再次使用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(); 
    } 
} 

這是從谷歌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 

這是日誌從谷歌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文件只包含「缺少符號列表」。值。

編輯2:我已經上傳到shared folder完整的Android項目,所以每個人都可以嘗試。您可以編譯和運行Android 2.x和4模擬器(或手機,如果您有:-))

編輯3:雖然這不是一個直接的答案,但它仍是一種解決方法。我使用谷歌貨幣計算器而不是雅虎。要使用谷歌貨幣計算器,只需使用以下代碼更改雅虎網址:http://www.google.com/ig/calculator?hl=en&q=1「+ fromCurrency +」=?「+ toCurrency。點擊this link查看將78歐元轉換爲美元的示例。 。但是如果有人發現爲什麼這是發生在雅虎網站,這將是很好的分享它與我們..

+0

發佈您的代碼。 –

+0

我已編輯我的答案(EDIT3)採用了使用Google貨幣計算器的解決方法。 – Dimitris

回答

1

嘗試刪除urlConnection.setDoOutput(true);當您在ICS中運行。因爲ICS變成GET請求時POST setDoOutput真)。

這個問題報道herehere

+0

是的!這是問題!非常感謝! – Dimitris