【发布时间】:2014-06-05 07:26:12
【问题描述】:
我正在使用 http 请求库来获取 xml 内容。 lib的监听器有以下功能:
void onDataReceived(char[] data)
{
}
void onRequestSucceeded()
{
}
请求一个url后,lib会分多条接收数据,当每条数据都接收到时,会调用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))
标签: android string stringbuffer