如何在Android中记录和解析InputStream

发布于 2024-12-05 10:43:20 字数 1148 浏览 0 评论 0原文

我用以下代码解析输入流,它工作正常。

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlHandlerMatchDetails handler = new XmlHandlerDetails();
xr.setContentHandler(handler);
xr.parse(new InputSource(data));

现在我需要记录InputStream来检查它,我以这种方式更改以前的代码:

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlHandlerMatchDetails handler = new XmlHandlerDetails();
xr.setContentHandler(handler);
xr.parse(new InputSource(data));

BufferedReader br = new BufferedReader(new InputStreamReader(data)); 
StringBuilder sb = new StringBuilder(); 
String line; 
while ((line = br.readLine()) != null) {     
   sb.append(line); 
} 
String text = sb.toString();
Log.i(mTag, text);

但我有错误

09-19 12:13:02.982: ERROR/MyClass(338): java.io.IOException: Attempted read on封闭流。

如何在不关闭流的情况下记录或将流转换为字符串?

编辑 - 已解决!!!

我通过解析字符串而不是 InputStream 来解决它

xr.parse(new InputSource(new StringReader(text)));

I parse an InputStream with following code and it works fine.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlHandlerMatchDetails handler = new XmlHandlerDetails();
xr.setContentHandler(handler);
xr.parse(new InputSource(data));

Now I need to log the InputStream to check it, I change the previous code in this way:

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlHandlerMatchDetails handler = new XmlHandlerDetails();
xr.setContentHandler(handler);
xr.parse(new InputSource(data));

BufferedReader br = new BufferedReader(new InputStreamReader(data)); 
StringBuilder sb = new StringBuilder(); 
String line; 
while ((line = br.readLine()) != null) {     
   sb.append(line); 
} 
String text = sb.toString();
Log.i(mTag, text);

But I have the error

09-19 12:13:02.982: ERROR/MyClass(338): java.io.IOException: Attempted read on closed stream.

How it's possible to log or to convert the stream to string without close it?

EDIT - SOLVED!!!

I solve it parsing the string instead of InputStream

xr.parse(new InputSource(new StringReader(text)));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2024-12-12 10:43:20

我不确定这就是问题所在,但您可以尝试将 while 循环重写为:

while( br.ready() )
  sb.append( br.readLine() );

我想到的另一件事是,您使用的 InputStream (数据)可能无法读取两次 -因此,首先使用 XmlReader 解析它会导致它关闭,然后当您尝试使用 BufferedReader 解析它时它不会打开。要验证这一点,请尝试运行 BufferedReader,而不首先使用 XmlReader 解析数据。

I'm not sure this is the problem, but you could try re-writing your while loop to this:

while( br.ready() )
  sb.append( br.readLine() );

Another thing that pops to my mind, is that the InputStream you use (data), might not be able to be read twice - thus parsing it with the XmlReader first causes it to close and then it will not be open when you try to parse it with the BufferedReader. To verify this, try to run the BufferedReader without first parsing the data with the XmlReader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文