Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yahoo Currency Exchange Rates in Android 4

In my application, I need to get the current exchange rates for different currencies. As it has been suggested by this, this and this question a nice way to do it, is to use the Yahoo Finance service.

So, when I want to find, for example, the rate between USD and Canadian dollars, I simply send this link: http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv

This is working fine for both my Motorola Atrix phone with Android 2.3.4 and the emulator again with Google API 2.3.3. However, when I try the exact same link from a Galaxy SII with Android ICS 4.0 and an emulator with Google API 4.0, in both cases the quotes.csv file contains only the "Missing Symbols List".

After digging around, I found out that this response can happen in case the rates were not found. But this response is for ANY currency I try under Android 4.0 (either Galaxy SII or emulator). Hence, I cannot get the rates with Android 4.0, but I can with Android 2.x.

Does anyone have experienced the same problem? Is there any workaround?

EDIT: This is the thread code that deals with downloading rates from the Yahoo Currency Service:

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

And this is my function for downloading the quotes.csv file from the Yahoo site:

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

This is the log from the 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

And this is the log from the 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.

As you can see the "fileLine" String variable, in the first case it gets the proper rates, while at the second, the quotes.csv file merely contains the "Missing Symbols List." value.

EDIT2: I have uploaded to a shared folder the complete Android project, so everyone can try it. You may compile and run with both Android 2.x and 4 emulators (or phones if you have :-))

EDIT3: Although this is not a direct answer, still it is a workaround. I use the Google currency calculator instead of the Yahoo one. To use the Google currency calculator, simply change the Yahoo url with this one: http://www.google.com/ig/calculator?hl=en&q=1" + fromCurrency + "=?" + toCurrency. Click this link to see an example of converting 78 Euros to USDs. I checked and it works with both Android versions. However if anyone finds out why this is happening with the Yahoo site, it would be good to share it with us..

like image 704
Dimitris Avatar asked Nov 13 '22 15:11

Dimitris


1 Answers

Try to remove urlConnection.setDoOutput(true); when you run in ICS. Because ICS turns GET request to POST when setDoOutput(true).

This problem is reported here and here

like image 136
iTech Avatar answered Nov 15 '22 05:11

iTech