2012-12-18 65 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