2

在执行我的项目时,我在 logcat 中收到如下所示的错误:

05-12 12:43:17.268:INFO/global(801):BufferedInputStream 构造函数中使用的默认缓冲区大小。如果需要 8k 缓冲区,最好是明确的。

我的代码如下所示。在这里,我传入的数据commonParser()是从 Web 服务获得的长响应。

public void commonParser(String data)
{
    try
    {
        if(data!=null)
        {
            InputStream is = new ByteArrayInputStream(data.getBytes());
            Reader reader = new InputStreamReader(is, "UTF-8");
            InputSource inputSource = new InputSource(reader);
            inputSource.setEncoding("UTF-8");
            SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
            sp.parse(inputSource, this);
        }
    } catch (UnsupportedEncodingException e) {
        System.out.println("Common Parser Unsupported Encoding :: "+e);
    } catch (ParserConfigurationException e) {
        System.out.println("Parse Config error"+e);
    } catch (SAXException e) {
        System.out.println("Sax error "+e);
    } catch (IOException e) {
        System.out.println("IO Error "+e);
    }
}

logcat 响应建议我使用 8k 缓冲区大小,但我不知道如何为BufferedInputStream.

4

2 回答 2

2

您可以尝试从InputStreamReader更改为BufferedReader 因为您已经在 InputSource 上设置了编码。然后你可以将缓冲区的大小设置为 8192(8k 这是 android 建议的)所以你的代码可能看起来像......

InputStream is = new ByteArrayInputStream(data.getBytes());
// use BufferedInputStream instead of InputStreamReader
BufferedInputStream bis = new BufferedInputStream(is, 8192);
InputSource inputSource = new InputSource(bis);
inputSource.setEncoding("UTF-8");
SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
sp.parse(inputSource, this);

...
于 2012-04-25T15:02:33.037 回答
0

您的代码没有任何问题,消息不是错误。将其视为一个 FYI,通知您可以指定缓冲区的大小,而不是使用默认的 8k。如果较小的尺寸很好,那么您可以节省一些内存空间

如果 8KB 恰好是您所需要的,您可以选择忽略警告,或者您可以使用构造函数调整大小以适应您的需要 - 尽可能小,尽可能大。

于 2012-08-18T08:05:48.510 回答