Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取HTTP Url连接中的错误_Java_Http_Url_Client - Fatal编程技术网

Java 获取HTTP Url连接中的错误

Java 获取HTTP Url连接中的错误,java,http,url,client,Java,Http,Url,Client,我正在为我的学校编写一个应用程序,我想展示网站上的新闻,所以我必须在我的应用程序中获取源代码。这是我从网站获取Html源代码的代码: public String getHTML(String urlToRead) { URL url; HttpURLConnection conn; BufferedReader rd; String line; String result = ""; try { url = new URL(urlT

我正在为我的学校编写一个应用程序,我想展示网站上的新闻,所以我必须在我的应用程序中获取源代码。这是我从网站获取Html源代码的代码:

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;
}
如果我有互联网连接,它可以正常工作,但如果没有连接,应用程序就会崩溃。如果没有连接到Internet,如何在应用程序中显示错误而不使其崩溃? (对不起我的英语,我是来自德国的学生…)

有人能帮我吗

谢谢


Jonathan

你需要抓住未知的后异常:

此外,我还将更改您的方法,使其仅从连接返回InputStream,并处理与之相关的所有异常。然后尝试读取或解析它,或者用它做任何其他事情。您可以获取errorInputStream并将对象状态更改为error(如果有)。你可以用同样的方法解析它,只需要做不同的逻辑

我想要更像:

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();
        }
    }
}

我希望它能帮助你,祝你的项目好运。

你需要抓住未知的例外:

此外,我还将更改您的方法,使其仅从连接返回InputStream,并处理与之相关的所有异常。然后尝试读取或解析它,或者用它做任何其他事情。您可以获取errorInputStream并将对象状态更改为error(如果有)。你可以用同样的方法解析它,只需要做不同的逻辑

我想要更像:

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();
        }
    }
}
我希望它能帮助你,祝你的项目好运