Currency Converter
Hey, I am writing a method that converts pounds to euros, and I am looking to connect to a website such as xe.com, passing the pound value to the website and parsing out the amount of euros from the result.
I have little idea how to go about this, and while I am not asking for anyone to write this for me (that would take the whole point out of programming), I was hoping you could perhap point me in the right direction to the parts of the API/reference websites I should be looking at.
Thank-you.
[518 byte] By [
abu5ea] at [2007-11-26 19:11:05]

Do you need to connect to a website? Could you not just enter the currency conversion rate in your application? The conversion rate will obviously change over time, so if you want the conversation rate to change when the actual rate does, perhaps you could check the conversion rate (using a website such as xe.com) to update the rate in your program, as opposed to connecting to the website from your program and such whenever anyone wants something converted.
I don't know how often xe updates the raters, but real rates can change several times per minute. (But the OP might not need that accuracy?)Kaj
The currency converter will be part of an RMI interface, so I don't really want the client to be able to change the rate.
While setting the conversion rate at the server end would be a possibility, and accuracy isn't really too important, I just thought it would be an interesting expansion, and might help me learn more.
If I would be easier, I know that http://www.google.co.uk/search?hl=en&q=pounds+to+euros&btnG=Google+Search&meta=
provides the current rate as it's first result, which could possibly be easier to parse.
My idea was to use a try catch with this, so it tried to connect to a website to calculate the rate, and the catch handled a hard-coded version (possibly with the most recently obtained exchange rate)
> I don't know how often xe updates the raters, but
> real rates can change several times per minute. (But
> the OP might not need that accuracy?)
>
> Kaj
That's true; I just assumed that type of accuracy would not be needed. If it is, then I suppose you would need to check the rate when a conversion is asked for. The problem with that is mainly the time it takes depending on one's connection speed.
While I know this is a tricky solution for a trivial problem, it is the concept I am more interested in, I am guessing that if I program for long enough, eventually I am going to need to connect to a website and parse information from it.
So, although it's not really necessary, I would appreciate it if you could point me in the right direction. I thought about looking at Sockets, but I had trouble getting anything useful out of those unless connecting to either LocalHost, or a remote computer supporting the socket, that had no firewall (which I don't expect will be true for most websites).
I could be wrong though, sockets might well do the trick. Any advice?
Thanks again.
This is what I found in their html:
<warning>
<!--
// *** LEGAL WARNING! ***
//
// IF YOU ARE READING THIS, YOU MAY BE CONSIDERING EXTRACTING RATE DATA FROM XE.COM.
// THIS IS A VIOLATION OF THE LEGAL TERMS OF USE. DATA EXCTRACTION IS NOT PERMITTED.
// REDISTRIBUTION OF THIS DATA IS PROHIBTED, AND ALL USE OF IT IS STRICTLY GOVERNED
// BY THE LEGAL TERMS OF USE AGREEMENT LOCATED AT:
//http://www.xe.com/legal/
// IF YOU NEED CURRENCY DATA FOR COMMERCIAL PURPOSES, WE CAN PROVIDE IT.
// FOR MORE INFORMATION, PLEASE SEE: http://www.xe.com/dfs/
//
// WE CAN AND DO PURSUE LEGAL ACTION AGAINST VIOLATORS OF THE TERMS OF USE AGREEMENT.
//
// -->
</warning>
kind regards,
Jos
Take a look at the URLConnection class.
> While I know this is a tricky solution for a trivial
> problem, it is the concept I am more interested in, I
> am guessing that if I program for long enough,
> eventually I am going to need to connect to a website
> and parse information from it.
It's not as common as you might think. The reason being that such applications are "fragile". They rely upon a 3rd party's implementation - when that 3rd party changes their implementation your code breaks, often without a clear cause.
Such applications are referred to as "screen scrapers", which I think conveys the slight sense of distaste that you'll feel when called upon to write code like this.
Not that this is any reason for you not to write it for your own interest. You should investigate the HttpConnection class, which provides most of what you'll be needing. You might also want to investigate the regex facilities (Pattern and Matcher, and some of the String methods).
If you're feeling particularly versatile you could try building and reading the page using DOM or JDOM (treating the page contents as XML).
(edit) I stand corrected - it is indeed URLConnection, although HttpConnection is relevant.
> While I know this is a tricky solution for a trivial
> problem, it is the concept I am more interested in, I
> am guessing that if I program for long enough,
> eventually I am going to need to connect to a website
> and parse information from it.
Probably not. It's something which you seldom do in commercial applications.
(I have written one small app that connects to this forum and generates some statistics but I think that that is the only time which I've had to connect to a site)
> So, although it's not really necessary, I would
> appreciate it if you could point me in the right
> direction. I thought about looking at Sockets, but I
> had trouble getting anything useful out of those
> unless connecting to either LocalHost, or a remote
> computer supporting the socket, that had no firewall
> (which I don't expect will be true for most
> websites).
>
> I could be wrong though, sockets might well do the
> trick. Any advice?
You can use sockets, and connect to port 80, but it's easier to use the URL class, and call openConnection.
Kaj
> Not that this is any reason for you not to
> write it for your own interest. You should
> investigate the HttpConnection class, which provides
> most of what you'll be needing. You might also want
> to investigate the regex facilities (Pattern and
> Matcher, and some of the String methods).
Another option is to use an URL and a Scanner. The Scanner can be used to apply regexp searches on the result.
Kaj
Thank-you for all your suggestions.I'll give it a go (redundant as it might be).
Whenever I try to connect with either HttpURLConnection (why Http isn't uppercase but URL is I'm not sure) or URLConnection I get a 403 response: Forbidden.
I am guessing this is more due to the way I am trying to connect than Google point blank refusing a connection, because I don't think they would want to prevent people writing web browsers in Java, so any idea what I am doing wrong?
I have:
URL u = new URL("http://www.google.co.uk/search?hl=en&q=pounds+to+euros&btnG=Google+Search&meta=");
HttpURLConnection connect = (HttpURLConnection) u.openConnection();
That's probably your proxy. Do a google for "URLConnection proxy" and you'll find some articles that cover using a proxy in this way.
Google Requires a User-Agent header to be present, and if it is not, you get the 403 error. add something like thisconnect.setRequestProperty("User-Agent", "NADA");
Thanks for the help guys.
> builder.append((char)i);
This is bad policy; you just copy the bytes to chars. That's the same as assuming the data uses the ISO-8859-1 charset. Now maybe your input does use that charset, I don't know. (Probably you don't know either, but you should find out or you may run into strange problems later.) In any case your assumptions ought to be obvious to the reader of your code instead of hidden by sneaky tricks. Like this:InputStream input = connect.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(input, "ISO-8859-1"));
String line;
while ((line = br.readLine()) != null) {
builder.append(line).append('\n');
}