繁体   English   中英

android concat字符串OutOfMemoryError

[英]android concat string OutOfMemoryError

我正在使用http请求库来获取xml内容。 库的侦听器具有以下功能:

void onDataReceived(char[] data)
{

}

void onRequestSucceeded()
{

}

请求url之后,lib将接收许多数据,当接收到onDataReceived数据时,将调用onDataReceived函数,并将该数据作为参数传递,我必须合并所有数据一串。 在请求完成后,将调用onRequestSucceeded函数,并且该字符串现在是xml的完整内容。

我正在这样做:

//init the result string
String resStr = new String("");

void onDataReceived(char[] data)
{
    resStr += new String(data);
}

void onRequestSucceeded()
{
    //now the resStr is ready for parse.
}

问题是,有时我的Android设备在确认字符串时会报告OutOfMemoryError。 所以我像这样更改为StringBuffer:

//init the result string
StringBuffer resStr = new StringBuffer("");

void onDataReceived(char[] data)
{
    resStr.append(data);
}

void onRequestSucceeded()
{
    //now the resStr is ready for parse.
}

但是resStr.toString()给了我诸如“ @bsdawevas”之类的怪异内容。 我怀疑编码有问题,但是我不知道如何解决。

有任何想法吗?

尝试resStr.append(new String(data));

采用

String str = resStr.toString();
System.out.println(str);

toString()将从StringBuffer转换为String对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM