【问题标题】:Get error in HTTP Url Connection在 HTTP URL 连接中获取错误
【发布时间】:2013-03-15 21:40:36
【问题描述】:

我正在为我的学校编写应用程序,并且我想显示来自网站的新闻,因此我必须在我的应用程序中获取源代码。这是我从网站获取 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;
}

如果我有互联网连接,它工作正常,但如果没有连接,应用程序崩溃。如果没有连接到互联网,我如何在应用程序中显示错误而不崩溃? (对不起我的英语,我是来自德国的学生......)

谁能帮帮我?

谢谢

乔纳森

【问题讨论】:

    标签: java http url client


    【解决方案1】:

    你需要捕捉 UnknownHostException:

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

    我会有更多类似的东西:

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

    希望它对您有所帮助,并祝您的项目好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 2018-09-11
      • 2017-05-20
      • 1970-01-01
      相关资源
      最近更新 更多