2012-12-18 8 views
5

In meiner Anwendung muss ich die aktuellen Wechselkurse für verschiedene Währungen erhalten. Wie es von this, this und this vorgeschlagen wurde, ist es eine gute Möglichkeit, den Yahoo Finance Service zu nutzen.Yahoo Währung Wechselkurse in Android 4

Also, wenn wir finden wollen, zum Beispiel die Rate zwischen USD und kanadischen Dollar, ich diesen Link einfach senden: http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv

Dies funktioniert gut für beide mein Motorola Atrix-Handy mit Android 2.3.4 und der Emulator erneut mit Google API 2.3.3. Wenn ich jedoch den gleichen Link von einem Galaxy SII mit Android ICS 4.0 und einem Emulator mit Google API 4.0 ausprobiere, enthält die Datei quotes.csv in beiden Fällen nur die Liste der fehlenden Symbole.

Nach dem Graben, fand ich heraus, dass diese Antwort passieren kann, falls die Raten nicht gefunden wurden. Aber diese Antwort ist für jede Währung, die ich unter Android 4.0 (entweder Galaxy SII oder Emulator) versuche. Daher kann ich die Preise nicht mit Android 4.0 bekommen, aber ich kann mit Android 2.x.

Hat jemand das gleiche Problem erlebt? Gibt es eine Problemumgehung?

EDIT: Dies ist der Thread-Code, der von dem Yahoo Währungs Dienst mit dem Herunterladen Preisen Angebote:

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

Und das ist meine Funktion der quotes.csv Datei aus der Yahoo-Website zum Download:

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

ist das Protokoll aus dem 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 

Und das ist das Protokoll aus dem Google-API 4.0-Emulator:

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. 

Wie Sie das „fileLine“ String-Variable, im ersten Fall sehen sie die richtigen Preise bekommen, während bei den zweiten, lediglich die quotes.csv Datei enthält die "Liste fehlender Symbole" Wert.

EDIT2: Ich habe das komplette Android-Projekt auf ein shared folder hochgeladen, damit jeder es ausprobieren kann. Sie können kompilieren und mit Android 2.x und 4 Emulatoren laufen (oder Telefone, wenn Sie haben :-))

EDIT3: Obwohl dies keine direkte Antwort ist, ist es immer noch ein Workaround. Ich benutze den Google-Währungsrechner anstelle von Yahoo. Um den Google-Währungsrechner zu verwenden, ändern Sie einfach die Yahoo-URL: http://www.google.com/ig/calculator?hl=en&q=1 "+ fromCurrency +" =? "+ ToCurrency. Klicken Sie this link, um ein Beispiel für die Umrechnung von 78 Euro in USD zu sehen. Ich habe es geprüft und es funktioniert mit beiden Android-Versionen . aber wenn jemand herausfindet, warum dies mit der Yahoo-Website geschieht, wäre es gut, es mit uns zu teilen ..

+0

berichtet wird, veröffentlichen Sie Ihren Code. –

+0

ich bearbeitet habe meine Antwort (EDIT3) mit einem Workaround, der den Google-Währungsrechner verwendet. – Dimitris

Antwort

1

Versuchen urlConnection.setDoOutput(true); zu entfernen, wenn Sie in ICS laufen. Da ICS Anfrage dreht GET POST wenn setDoOutput (true).

Dieses Problem here und here

+0

Ja! Das war das Problem! Danke vielmals! – Dimitris