-1

i new to java ME, and i want to create an app that can get the latest exchange rates for a paticular currency online, how can i get this done?

I've been able to create an HttpConnection object and retrieve data from a URL, but it doesn't work for google.

i saw an example here from a question here titled How to get a live exchange rate data and apply it to an Android App. However, if i try it on a mobile app it returns nothings. how can i work around this?

String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";//test URL
c = (HttpConnection)Connector.open(url);
s = c.openInputStream();//open an input stream
if( l != -1 ) {
  for (i =0 ; i < l ; i++ )
  if((ch = s.read()) != -1){
     b.append((char) ch);
    }
}
stringItem2.setText("\n " + b.toString());

With

String url = "http://developers.sun.com/mobility/midp/articles/event/EventEx1.java";

i am able to get the website data, but with

String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";

i get nothing

Community
  • 1
  • 1
TiOLUWA
  • 207
  • 4
  • 15

2 Answers2

1

I would use http://xurrency.com/api/ to retrieve up-to-date exchange rates, you have to pay 29.99 for this though if you are querying it more than 10 times per day. The documentation part of the page explains how to use the API. E.g. to retrieve the US to EURO exchange you use http://xurrency.com/api/usd/eur/1. If you enter this into your browser and view the source, you will see the response retrieved. The response is in JSON.

You will need to use something like GSON to convert the JSON data into a Java object. The code to do this should be pretty simple, something like:

    URL url = new URL("http://xurrency.com/api/usd/eur/1");
    URLConnection yc = url.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            yc.getInputStream()));
    String jsonObject = "";
    String line;
    while ((line = in.readLine()) != null) 
        jsonObject += line;

    in.close();
    Gson gson = new Gson();
    ExchangeRate = gson.fromJson(jsonObject, ExchangeRate.class);

The ExchnageRate class would be a custom class designed to encapsulate the data retrieved from this JSON API.

You can make the above code work with the link you are using. Try the following code:

  import com.google.gson.Gson;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.URL;
  import java.net.URLConnection;

  public class Main {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD");
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            yc.getInputStream()));
        String jsonObject = "";
        String line;
        while ((line = in.readLine()) != null) 
            jsonObject += line;

        in.close();
        System.out.println(jsonObject);
        Gson gson = new Gson();
        GoogleCurrency another = gson.fromJson(jsonObject, GoogleCurrency.class);
        another.print();
    }

  }


  public class GoogleCurrency {
     private String lhs;
     private String rhs;
     private String error;
     private String icc;

   public GoogleCurrency() {
    lhs = "0 U.S Dollar";
    rhs = "0 U.S Dollar";
    error = "true";
    icc = "false";
   }

   public GoogleCurrency(String lhs,String rhs,String error,String icc) {
    this.lhs = lhs;
    this.rhs = rhs;
    this.error = error;
    this.icc = icc;
   }

   public void print() {
    System.out.println(lhs + " = " + rhs);
   }
 }
GordyD
  • 5,063
  • 25
  • 29
  • okay thanks alot. Does this imply it's not possible to do this with google, because it looks a whole lot easier? – TiOLUWA Jul 06 '11 at 10:23
  • I have never seen the Google calculator before but it will work fine. It is just a question of whether using the data from this URL for your app is breaking the terms of use that Google have set out. – GordyD Jul 06 '11 at 10:35
  • okay, i've heard that terms of use issue before with google, this approach works fine for me, thanks alot, you solved my problem. – TiOLUWA Jul 06 '11 at 11:13
0

Please find the below code which returns json response for getting Conversion Rate.

    HttpClient client = new HttpClient();       

    NameValuePair arg1 = new NameValuePair("method","runJob");

    //Change your currency types here in which you would want to convert

    NameValuePair arg2 = new NameValuePair("from","USD");
    NameValuePair arg3 = new NameValuePair("to", "PKR");

    //getting the method
    GetMethod method = new GetMethod("http://rate-exchange.appspot.com/currency");
    method.setQueryString(new NameValuePair[]{arg1, arg2, arg3});

    //executes the link
    client.executeMethod(method);

    //getting response in string
    JSONObject obj = new JSONObject(method.getResponseBodyAsString());

    //getting rate from the json response
    double rate = obj.getDouble("rate");

    //closing conncetion
    method.releaseConnection();     


    //returning value
    return rate;
Hasni
  • 1
  • 2