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?
Asked
Active
Viewed 6.3k times
3 Answers
51
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.

Praveenkumar
- 24,084
- 23
- 95
- 173

Drew
- 2,269
- 21
- 16
-
4Thanks, I tried that before and I just realized my problem: I forgot to give it internet permissions... – user200565 Jan 16 '10 at 01:54
-
2If 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 Jan 16 '10 at 02:10
-
can you get it all in one string at once or do you have to append it line by line? – gonzobrains Jul 14 '11 at 12:36
-
2since API 11 remeber about http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Selvin Sep 17 '13 at 15:28
-
2the code should be inside AsyncTask or any background thread. – iDeveloper Oct 18 '17 at 12:55
6
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();

nurnachman
- 4,468
- 2
- 37
- 40
-
1How would I write the try-catch block? I'm pretty new and Java is telling me to handle exceptions. Also: fullString is the output, isn't it? – Saman Miran Apr 25 '16 at 09:16
-
@SamanMiran try-catch is used in java for doing something that might raise an exception, and catching that exception. try {// do something risky } catch (Exception exception) {// handle it} – nurnachman Apr 28 '16 at 12:15
-
@SamanMiran yes - fullString is the output; you can see that it starts as an empty string ("") and in every loop of the while block - another line is being added to it in the end – nurnachman Apr 28 '16 at 12:16
-
Using String like that is very bad: you allocate a new String object in every step of the loop. Use a StringBuilder instead, and append to it. Use StringBuilder.toString() to get the resulting String. – noamtm Dec 28 '17 at 11:59
5
You can put it in an AsyncTask like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
try {
new Main2Activity.MyTask().execute(this);
} catch (Exception e) {
e.printStackTrace();
}
}
private static class MyTask extends AsyncTask<Object, Void, String> {
Main2Activity activity;
@Override
protected String doInBackground(Object... params) {
activity = (Main2Activity)params[0];
try {
StringBuilder sb = new StringBuilder();
URL url = new URL("http://www.google.com/");
BufferedReader in;
in = new BufferedReader(
new InputStreamReader(
url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
sb.append(inputLine);
in.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String str) {
//Do something with result string
WebView webView = activity.findViewById(R.id.web_view);
webView.loadData(str, "text/html; charset=UTF-8", null);
}
}

live-love
- 48,840
- 22
- 240
- 204