I'm working on an Application for my School, and I want to show the news from the Website, so I have to get the Sourcecode in my Application. This is my Code for getting the Html- Sourcecode from the Website:
public String getHTML(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
result += e.toString();
}
return result;
}
If I have an Internet connection, it works fine, but if there's no connection, the App crashes. How can I show an Error in the App, if there's no connection to the Internet, without crashing it? (Sorry for my English, I'm a pupil from Germany...)
Can anyone help me?
Thanks
Jonathan