String getText(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
//add headers to the connection, or check the status if desired..
// handle error response code it occurs
int responseCode = conn.getResponseCode();
InputStream inputStream;
if (200 <= responseCode && responseCode <= 299) {
inputStream = connection.getInputStream();
} else {
inputStream = connection.getErrorStream();
}
BufferedReader in = new BufferedReader(
new InputStreamReader(
inputStream));
StringBuilder response = new StringBuilder();
String currentLine;
while ((currentLine = in.readLine()) != null)
response.append(currentLine);
in.close();
return response.toString();
}
This will download text data from the specified URL, and return it as a String.
How this works:
HttpUrlConnection from our URL, with new URL(url).openConnection(). We cast the UrlConnection this returns to a HttpUrlConnection, so we have access to things like adding headers (such as User Agent), or checking the response code. (This example does not do that, but it’s easy to add.)InputStream basing on the response code (for error handling)BufferedReader which allows us to read text from InputStream we get from the connection.StringBuilder, line by line.InputStream, and return the String we now have.Notes:
IoException in case of failure (such as a network error, or no internet connection), and it will also throw an unchecked MalformedUrlException if the given URL is not valid.Usage:
Is very simple:
String text = getText(”<http://example.com>");
//Do something with the text from example.com, in this case the HTML.