从BufferedReader读取时,找不到java.io.EOFException:\ n

时间:2017-01-02 13:57:03

标签: java android android-7.0-nougat

我正在尝试读取rss feed但在尝试从BufferedReader读取时遇到错误。

Error stack trace [screenshot]

以下是读取和转换rss feed的方法

private String downloadXML(String urlPath){
        StringBuilder xml = new StringBuilder();
        try {
            URL url = new URL(urlPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int responseCode = connection.getResponseCode();
            Log.d(TAG, "downloadXML: The response code was " + responseCode);
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            int charRead;
            char [] bufferRead = new char[500];
            while (true){
                charRead = reader.read(bufferRead);

                if(charRead<0){
                    break;
                }
                if(charRead>0){
                    xml.append(String.copyValueOf(bufferRead, 0, charRead));
                }
            }
            reader.close();

            return xml.toString();
        }
        catch (MalformedURLException e){
            Log.e(TAG, "downloadXML: Invalid URL " + e.getMessage() );
        }
        catch (IOException e){
            Log.e(TAG, "downloadXML: IO exception " + e.getMessage() );
            e.printStackTrace();
        }
        catch (SecurityException e) {
            Log.e(TAG, "downloadXML: SecurityEception " + e.getMessage() );
        }
        return null;
    }

第62行是charRead = reader.read(bufferRead); 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我们的项目遇到了类似的问题。我们的解决方案是为http标头添加配置。 addHeader(“连接”,“关闭”)