2012-12-18 19 views
5

W mojej aplikacji potrzebuję uzyskać aktualne kursy wymiany dla różnych walut. Zgodnie z sugestią, jaką można sobie wyobrazić, dobrym pomysłem jest skorzystanie z usługi Yahoo Finance.Stawki wymiany walut Yahoo w systemie Android 4

Tak więc, gdy chcę znaleźć, na przykład, stopa między USD i dolarach kanadyjskich, po prostu wysłać link: http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv

to działa dobrze zarówno dla mojego telefonu Motorola Atrix z Androidem 2.3.4 i ponownie emulator z API Google 2.3.3. Jednak gdy wypróbuję dokładnie ten sam link z Galaxy SII z Androidem ICS 4.0 i emulatorem z Google API 4.0, w obu przypadkach plik quotes.csv zawiera tylko "Listę brakujących symboli".

Po przekopaniu dowiedziałem się, że ta odpowiedź może się zdarzyć w przypadku, gdy stawki nie zostały znalezione. Ale ta odpowiedź dotyczy DOWOLNEJ waluty, którą wypróbuję w systemie Android 4.0 (albo Galaxy SII, albo emulator). Dlatego nie mogę uzyskać stawek w systemie Android 4.0, ale mogę z Androidem 2.x.

Czy ktoś doświadczył tego samego problemu? Czy istnieje jakieś obejście?

EDIT: Jest to kod nić, która zajmuje się pobieraniem kursów walut z Yahoo usługę:

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

i to jest moja funkcja pobierania pliku quotes.csv z serwisu Yahoo:

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

jest to dziennik z Google API 2.3.3 emulator:

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 

i to jest log z API 4.0 emulatora Google:

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. 

Jak widać „fileLine” zmiennej String, w pierwszym przypadku robi się odpowiednie stawki, natomiast w drugim, plik zawiera jedynie quotes.csv "Lista brakujących symboli". wartość.

EDIT2: Przesłałem do shared folder kompletny projekt Androida, aby wszyscy mogli go wypróbować. Możesz skompilować i uruchomić zarówno emulatory Android 2.x, jak i 4 (lub jeśli masz :-))

EDIT3: Chociaż nie jest to bezpośrednia odpowiedź, nadal jest to obejście tego problemu. Używam kalkulatora walut Google zamiast Yahoo. Aby skorzystać z kalkulatora walutowego Google, po prostu zmień adres URL Yahoo: http://www.google.com/ig/calculator?hl=en&q=1 "+ fromCurrency +" =? "+ Na waluta Kliknij na przykład, aby przekonwertować 78 euro na USD. Sprawdziłem i działa on w obu wersjach Androida Jeśli jednak ktoś dowie się, dlaczego tak się dzieje z witryną Yahoo, dobrze byłoby podzielić się nią z nami ..

+0

pisać kod. –

+0

i edycja moja odpowiedź (EDIT3) z obejściem, które wykorzystuje kalkulator walutowy Google. – Dimitris

Odpowiedz

1

Spróbuj usunąć urlConnection.setDoOutput(true); podczas uruchamiania w ICS, ponieważ ICS zamienia żądanie GET na POST, gdy setDoOutput (true).

Ten problem jest zgłaszane here i here

+0

Tak! To był problem! Wielkie dzięki! – Dimitris