【问题标题】:java.io.EOFException: \n not found, while reading from BufferedReaderjava.io.EOFException: \n 未找到,同时从 BufferedReader 读取
【发布时间】:2017-01-02 13:57:03
【问题描述】:

我正在尝试读取 rss 提要,但尝试从 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); 任何帮助将不胜感激。

【问题讨论】:

  • 尝试将while(true)更改为while(reader.readLine() != null)
  • 我试过了,但同样的错误。
  • 尝试使用此代码将行号:10 更改为 22 String str; while ((str=reader.readLine()) != null) { xml.append(str); } reader.close();
  • java.io.EOFException: \n not found: size=0 我在你建议的更改后发现了这个异常
  • 这看起来像是 Android 中的一个主要错误。如果您没有,它就没有任何业务阅读线,也没有任何业务抱怨缺少换行符。注意charRead 不可能为零,除非buffer 的长度为零,但事实并非如此。不要编写无意义的测试。

标签: java android android-7.0-nougat


【解决方案1】:

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

【讨论】:

  • 你救了我的命
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
相关资源
最近更新 更多