2012-12-18 14 views
5

Nella mia domanda, ho bisogno di ottenere i tassi di cambio correnti per diverse valute. Come è stato suggerito da this, this e this domanda un buon modo per farlo, è quello di utilizzare il servizio di finanza di Yahoo.Tassi di cambio Yahoo in Android 4

Così, quando ho voglia di trovare, per esempio, il rapporto tra dollaro e dollari canadesi, ho semplicemente inviare questo link: http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv

Questo sta lavorando bene sia per il mio telefono Motorola Atrix con Android 2.3.4 e l'emulatore di nuovo con Google API 2.3.3. Tuttavia, quando provo lo stesso identico link da Galaxy SII con Android ICS 4.0 e un emulatore con Google API 4.0, in entrambi i casi il file quotes.csv contiene solo la "Lista dei simboli mancanti".

Dopo aver scavato, ho scoperto che questa risposta può accadere nel caso in cui i tassi non sono stati trovati. Ma questa risposta è per QUALSIASI valuta che provo con Android 4.0 (Galaxy SII o emulatore). Quindi, non posso ottenere le tariffe con Android 4.0, ma posso con Android 2.x.

Qualcuno ha riscontrato lo stesso problema? C'è qualche soluzione?

EDIT: Questo è il codice filo che si occupa di tassi di download dal Yahoo valuta Servizio:

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

E questa è la mia funzione per il download quotes.csv dal sito di 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(); 
    } 
} 

questo è il log dalle API di Google 2.3.3 dell'emulatore:

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 

E questo è il registro da Google API 4.0 emulatore:

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. 

Come si può vedere la variabile String "Fileline", nel primo caso si ottiene i tassi adeguati, mentre al secondo, il file contiene solo quotes.csv la "Lista dei simboli mancanti". valore.

EDIT2: ho caricato su un shared folder il progetto Android completo, in modo che tutti possano provarlo. Puoi compilare ed eseguire entrambi gli emulatori Android 2.xe 4 (o telefoni se hai :-))

EDIT3: Anche se questa non è una risposta diretta, è comunque una soluzione. Io uso il calcolatore di valuta di Google invece di quello di Yahoo. Per utilizzare il calcolatore di valuta di Google, basta cambiare l'url di Yahoo con questo: http://www.google.com/ig/calculator?hl=en&q=1 "+ fromCurrency +" =? "+ ToCurrency. Fai clic su this link per vedere un esempio di conversione da 78 euro a USD. Ho controllato e funziona con entrambe le versioni di Android . Tuttavia, se qualcuno scopre perché questo sta accadendo con il sito di Yahoo, sarebbe bello condividere con noi ..

+0

pubblicare il tuo codice. –

+0

ho modificato la mia risposta (EDIT3) con una soluzione alternativa che utilizza il calcolatore di valuta di Google. – Dimitris

risposta

1

tenta di rimuovere urlConnection.setDoOutput(true); quando si esegue in ICS. perché ICS si trasforma GET richiesta di POST quando setDoOutput (vero).

Questo problema viene segnalato here e here

+0

Sì! Questo era il problema! Molte grazie! – Dimitris