【问题标题】:IOException: stream is closed, NullPointerException with HttpURLConnection disconnectIOException:流已关闭,NullPointerException 与 HttpURLConnection 断开连接
【发布时间】:2018-02-01 21:19:36
【问题描述】:

每个人。我正在编写一个使用类 HttpURLConnection 连接到服务器的函数。在代码中,我建立了一个连接,依次调用getOutputStream()和getInputStream()方法。然后我断开连接。之后尝试获取getInputStream()方法获取的数据,编译器提示NullPointerException。

代码如下:

    DataOutputStream out = null;
    InputStreamReader inStrReader = null;
    BufferedReader reader = null;
    HttpURLConnection connection = null;

    try {
        URL postUrl = new URL(null, url, new sun.net.www.protocol.https.Handler());
        connection = (HttpURLConnection) postUrl.openConnection();
        ...//some setting methods
        connection.connect();
        out = new DataOutputStream(connection.getOutputStream());
        out.writeBytes(JSONObject.toJSONString(param));
        out.flush();
        out.close();

        inStrReader = new InputStreamReader(connection.getInputStream(), "utf-8");
        reader = new BufferedReader(inStrReader);
        connection.disconnect();   //<--HERE, release the connection
        StringBuilder stringBuilder = new StringBuilder();
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {     //<--null pointer
            stringBuilder.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (inStrReader != null) {
            try {
                inStrReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

调试尝试后,当我将断开连接线移动到 finally 模块的最后一行时,一切都会好起来的。但是我很困惑,当我已经将 'inputstream' 值分配给 'reader' 时会发生这种情况。

非常感谢。

【问题讨论】:

    标签: java nullpointerexception connection httpurlconnection inputstreamreader


    【解决方案1】:

    赋值不等于读取,reader.readLine()从连接开始读取。

    InputStreamReader 正在使用连接读取字节,在它使用连接读取字节之前断开连接

    InputStreamReader 是从字节流到字符的桥梁 流:它读取字节并...

    【讨论】:

    • 在断开连接之前,我已经将输入流对象放入变量“阅读器”中。但是,当我调试时,reader.readLine() 方法会得到 nullpointexception。
    • 但是你什么都没读,为了阅读,连接应该是打开的,因为你正在使用它阅读
    【解决方案2】:

    记住它是一个“流”。您需要有一个活动连接才能从流中读取。仅在从流中检索数据后关闭连接。

    【讨论】:

    • 我明白了。感谢您的明确解释。
    【解决方案3】:

    你做所有事情的顺序都错了。这没有意义。

    1. 您正在断开连接,然后希望能够从连接中读取。这里完全废话。通常你根本不应该断开连接,因为你会干扰 HTTP 连接池。只需将其删除,或者,如果您必须拥有它,请在所有关闭后进行。

    2. 您关闭的顺序错误,但您根本不需要关闭 inStrReader。关闭 BufferedReader 就可以了。只需删除 inStrReader.close() 的所有代码即可。

    3. 您将关闭out 两次。不要那样做。

    4. connect() 隐式发生。你不需要自己调用它。

    5. new URL(url) 就足够了。自 2003 年左右以来,您无需提供 HTTPS Handler

    【讨论】:

    • 感谢您的建议。我维护此代码只是因为 HP fortify(HP 企业安全产品)。如果我删除 finally 模块中的 close() 方法,那么我将收到一些未释放资源的警告:流。
    • 那么不要将InputStreamReader 声明为变量。只需new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))
    • 这是初始代码,fortify 报告未发布的资源:stream.然后我将 InputStreamReader 声明为变量。
    • 所以关闭它,没关系,但不要在BufferedReader之前。使用 try-with-resources 让这一切变得更干净。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多