Using URL me = new URL("http://www.google.com")
I've imported java.net, and I'm trying to open a connection to a website. When I try the following line:
URL me =new URL("http://www.google.com")
I get the compilation error:
java:157: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown.
This is the onyl line in the function, so what could I be doing wrong?
Thanks for any help
# 1
First of all, theres no semi-colon after that line. Second of all, that constructor throws a MalformedURLException, which needs to be caught.
try
{
URL me = new URL("http://www.google.com");
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
# 2
K thanks, another problem I'm having now is a compilation warning, I'm using Java 1.5, and using the following code:
InputStream is = null;
DataInputStream dis;
String s = "a";
try{
URL url = new URL(s1);
is = url.openStream();
dis = new DataInputStream(new BufferedInputStream(is));
while ((s = dis.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception e){
}
I get the warning:
java:164: warning: [deprecation] readLine() in java.io.DataInputStream has been deprecated
while ((s = dis.readLine()) != null) {
^
1 warning
Any ideas?