tags:

views:

83

answers:

1

I'm new to android and I'm trying to figure out how to get the contents of a URL as a String. For example if my URL is http://www.google.com/ I want to get the HTML for the page as a String. Could anyone help me with this?

+3  A: 

From the Java Docs: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html

URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
            new InputStreamReader(
            yahoo.openStream()));

String inputLine;

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

in.close();

Instead of writing each line to System.out, just append it to a string.

Drew
Thanks, I tried that before and I just realized my problem: I forgot to give it internet permissions...
If you post code examples which seem to be doing the right thing, it gives everyone else a chance to figure out what else could be causing the problem (like your permission issue).
Drew