使用 AsyncTask 关闭 InputStream

closing InputStream with AsyncTask

我有以下代码。它运行良好,但我意识到我从未关闭 InputStream。经过研究,我似乎应该在我的一个捕获之后添加一个 finally 来关闭它。

Q1:我需要关闭InputStream吗?

Q2:如果是,怎么做?

这是我的代码示例:

    public void refreshWind (final double lat, final double lon){

    new AsyncTask<String, Void, String>() {
        @Override
        protected String doInBackground(String... strings) {

            String YQL = String.format("select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text=\"(%s,%s)\")", lat, lon);
            String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));

            try {
                URL url = new URL(endpoint);
                URLConnection connection =url.openConnection();
                connection.setReadTimeout(10 *1000);         
                connection.setConnectTimeout(10 *1000);      
                InputStream inputStream =connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) !=null){
                    result.append(line);
                }
                return result.toString();
            }catch (Exception e) {
                error = e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {

            if (s ==null && error !=null){
                callback.serviceFailure(error);
                return;
            }
            try {
                JSONObject data = new JSONObject(s);
                JSONObject queryResults =data.optJSONObject("query");
                int count =queryResults.optInt("count");
                if (count ==0){
                    callback.serviceFailure(new LocationWeatherException("Wind information not available at this time."));
                    return;
                }

                Channel channel = new Channel();
                channel.populate(queryResults.optJSONObject("results").optJSONObject("channel"));
                callback.serviceSuccess(channel);
            }catch (JSONException e) {
                callback.serviceFailure(e);
            }
        }
    }.execute();

}

我认为你应该在包装你的 InputStreamBufferedReader 上调用 close()。如果发生异常,您应该在 finally 关闭时调用它:

protected String doInBackground(String... strings) {

    String YQL = String.format("select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text=\"(%s,%s)\")", lat, lon);
    String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));
    BufferedReader reader = null;

    try {
        URL url = new URL(endpoint);
        URLConnection connection =url.openConnection();
        connection.setReadTimeout(10 *1000);         
        connection.setConnectTimeout(10 *1000);      
        InputStream inputStream =connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder result = new StringBuilder();
        String line;
        while ((line = reader.readLine()) !=null){
            result.append(line);
        }
        return result.toString();
    } catch (Exception e) {
        error = e;
    } finally {
        // should have no effect if stream already closed
        if (reader != null) {
            reader.close();
        }
    }
    return null;
}

来自Javadoc for BufferedReader#close()

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.