Getting Text from a URL

try {
    // Create a URL for the desired page
    URL url = new URL("http://hostname:80/index.html");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

Comments

4 Feb 2010 - 1:46am by afroz arshee (not verified)

this code gives us the html formatted text but whr in i want the acctual text
how do i get it plz reply to me as soon as possible to afreaz@gmail.com

1 Mar 2010 - 10:36am by vmollov (not verified)

I tried this and it only works if the URL is on the same domain as the JSP page. Does anyone know a workaround for that problem?
Thank you!

23 May 2010 - 6:39am by THANX (not verified)

haha,exactly what i wanted,thanks a lot.keep it up!!

28 Oct 2010 - 2:27am by lincy (not verified)

very useful

1 Nov 2010 - 9:07am by Anonymous (not verified)

nolmus buraya aq
kod ararken
ortalik reklam dolmus

31 Dec 2010 - 2:52am by Cthulhu (not verified)

Note that this returns the contents on a per-line basis. If you want to just get the whole contents of the file, it's better to use the read() method with a char array buffer.

10 Aug 2011 - 4:37pm by Anonymous (not verified)

char array has size limit since array size is int. the max is Integer.MAX_VALUE.

11 Aug 2011 - 12:52am by sivakumar (not verified)

I got timeout exception for this code.
Please sent me a suggestion for my mail Id sivakumar0211@gmail.com

31 Aug 2011 - 1:45pm by ostracer (not verified)

thx!
it works!

31 Aug 2011 - 1:45pm by ostracer (not verified)

thx!
it works!

5 Oct 2011 - 12:18am by moncler jackets (not verified)

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for?

25 Oct 2011 - 1:12am by venugopal peparthi (not verified)

hi friends iam new to android ,how to set text to textview dynamically from server in android.please Help me friends iam strgulling a lot on this issue.

2 Nov 2011 - 5:26am by Motorcycle Clothing (not verified)

I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for?

15 Jan 2012 - 10:12pm by Guest (not verified)

It loops reading each line, and stores only the LAST line into str?

1 Feb 2012 - 8:04am by Guest (not verified)

while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}

Read 1 line at a time... then destroy that line... while you read each additional line... overwriting it.

When you are done... you only have line 143. The previous 142 lines have been lost.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.