Question

I'm developing a foreign exchange rate app that downloads data from a specific url and then search for a string that contains an ISO code that identifies each currency and its value in another currency. I've developed this in python, but I'm trying to port it to Java and subsequently to Android.

Python Code: Using urllib(2)

import urllib
import urllib2
from urllib import urlencode
from urllib2 import urlopen
from encodings import utf_8
import os

pg = urlopen("http://rss.timegenie.com/forex.txt")
text = pg.read().decode("utf8")

usd = text.find('USD')
start_of_priceusd = usd + 25
end_of_priceusd = start_of_priceusd + 6
priceusd = float(text[start_of_priceusd:end_of_priceusd])
.
.
.

The above code is correct and functional on Python and in SL4A in Android, now i'm trying to port that in Java and the Android SDK.

So far I've written this in standard Java:

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {
  URL curr = new URL("hhttp://rss.timegenie.com/forex.txt");
  BufferedReader in = new BufferedReader(
        new InputStreamReader(
        curr.openStream()));

  String inputLine;

  while ((inputLine = in.readLine()) != null)
      System.out.println(inputLine);

  in.close();
    }
}

So, how can i apply string manipulation to get the values depicted in the URL in a float variable?

Sorry for my English.

Was it helpful?

Solution

List<Float> priceList = new ArrayList<Float>();
     while ((inputLine = in.readLine()) != null){
         System.out.println(inputLine);
         String usd = inputline.substring(inputline.indexOf("USD")+25,inputline.length);     
         Float f = Float.valueOf(usd);
         priceList.add(f);              
        }
      in.close();

return priceList;

hope this will give you some insight

OTHER TIPS

Have a look at StringBuilder and it's indexOf() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top