Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read contents of a URL in Android

Tags:

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?

like image 621
user200565 Avatar asked Jan 16 '10 01:01

user200565


2 Answers

From the Java Docs : readingURL

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.

like image 98
Drew Avatar answered Oct 06 '22 01:10

Drew


You can open a stream and read and append each line to a string - remember to wrap everything with a try-catch block - hope it helps!

String fullString = ""; URL url = new URL("http://example.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = reader.readLine()) != null) {     fullString += line; } reader.close(); 
like image 42
nurnachman Avatar answered Oct 05 '22 23:10

nurnachman