Hi and I have made a simple Java function/method and for some reason it is not working. It doesn't show errors. It doesn't display results. It returns nothing and forces other things around it to do nothing. Below is my script.

private String get_url(String address) {
    String result="";
        try {
            URL yahoo = new URL(address);
            URLConnection yahooConnection = yahoo.openConnection();
            //DataInputStream dis = new DataInputStream(yahooConnection.getInputStream());
            BufferedReader dis = new BufferedReader(new InputStreamReader(yahooConnection.getInputStream()));
            String inputLine;
            while ((inputLine = dis.readLine()) != null) {
                result=result+inputLine+"\n";
            }
            dis.close();
        } catch (MalformedURLException me) {
        } catch (IOException ioe) {
        }
    return result;
    }

And to call it I used the following:

jTextArea1.setText("-"+get_url("http://google.com.au/"));

But yet it doesn't even show that dash. And as soon as I remove the get_url() function the dash will display. It should display the html code of Googles homepage. Can anybody see what is wrong with my code as there are no errors reported and the function just won't display the html code...

There are no errors displayed because you are catching the exceptions and not doing anything with them. Try this to see what is happening:

} catch (MalformedURLException me) {
  System.err.println(me.getMessage());
} catch (IOException ioe) {
  System.err.println(ioe.getMessage());
}

Just tried your code and still nothing... My code makes a Java Applet and not an Application so does that make a difference as to how your code should be done. Also I tried sending the error messages to a text box and they didn't display neither. Please help...

There are no errors displayed because you are catching the exceptions and not doing anything with them. Try this to see what is happening:

} catch (MalformedURLException me) {
  System.err.println(me.getMessage());
} catch (IOException ioe) {
  System.err.println(ioe.getMessage());
}

A security vulnerability in the Java Runtime Environment may allow code loaded from the local filesystem to access localhost:

I suspect you're running the applet from a local HTML (say, sample.htm) file.

Well what I wanted to do was use java the same way you would use ajax. But I am having problems connecting/streaming to external webpages using a Java applet. Anybody know the solution?

I wrote a console app and printed the HTML output. This is 6048 number of characters, all plain HTML. So there is no problem with the method get_url.
I disagree with above (adatapost) post because you are seeing the output of "-". that leaves 2 possibilities: 1. jTextArea1 has character limit or special characters/char combination or quotesare causing trouble
2. For applet, for security reasons, URL connection to third party site is being stopped. Thouh I would expect exception in that case.

I suggest, for test sake, return a huge hard coded string from get_url and see if setText still works.
If that works, see if current get_URL works for you in console mode like I idid.
If that works, see if some local URL can be used with applet.
All the best

This article has been dead for over six months. Start a new discussion instead.