Get error in HTTP Url Connection

2019-08-27 19:44发布

问题:

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

回答1:

You need to catch UnknownHostException:

As well I would change your method to return only InputStream from a connection and handle all exceptions related with it. Only then try to read or parse it or do anything else with it. You can get an errorInputStream and change object state to error, if any. You can parse it same way, just do different logic.

I would have something more like :

public class TestHTTPConnection {

    boolean error = false;

    public InputStream getContent(URL urlToRead) throws IOException {
        InputStream result = null;
        error = false;
        HttpURLConnection conn = (HttpURLConnection) urlToRead.openConnection();
        try {
            conn.setRequestMethod("GET");
            result = conn.getInputStream();
        } catch (UnknownHostException e) {
            error = true;
            result = null;
            System.out.println("Check Internet Connection!!!");
        } catch (Exception ex) {
            ex.printStackTrace();
            error = true;
            result = conn.getErrorStream();
        }
        return result;
    }

    public boolean isError() {
        return error;
    }

    public static void main(String[] args) {
        TestHTTPConnection test = new TestHTTPConnection();
        InputStream inputStream = null;
        try {
            inputStream = test.getContent(new URL("https://news.google.com/"));
            if (inputStream != null) {
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        inputStream));
                StringBuilder data = new StringBuilder();
                String line;
                while ((line = rd.readLine()) != null) {
                    data.append(line);
                    data.append('\n');
                }
                System.out.println(data);
                rd.close();
            }
        } catch (MalformedURLException e) {
            System.out.println("Check URL!!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I hope it will help you and good luck with your project.