我正在编写一个Android应用程序,它加载HTML页面并从中获取特定数据。我用android 4.0测试过,一切都很好。今天我试图在Android 2.2(froyo)上运行它,并出乎意料地面临严重的性能泄漏。我的代码如下:
ArrayList<News> news = new ArrayList<News>();
HttpPost httpPost = new HttpPost(BASE_URL
+ "news_view.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("start", String
.valueOf(start)));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = getHttpClientInstance().execute(httpPost);
HtmlCleaner cleaner = new HtmlCleaner();
String s = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
TagNode root = cleaner.clean(s);
TagNode[] list = root.getAllElements(false)[1].getAllElements(false);
if (list.length == 0)
throw new ParsingException();
for (int i = 0; i < list.length; i++) {
String header = list[i].getElementsByName("h3", true)[0].getText()
.toString();
TagNode footer = list[i].getElementsByName("h3", true)[1];
String date = footer.getElementsByName("span", true)[0].getText()
.toString();
String author = footer.getElementsByName("a", true)[0].getText()
.toString();
String target = footer.getElementsByName("a", true)[1]
.getAttributeByName("href");
(!) String text = list[i].getText().toString().replace(header, "")
.replace(footer.getText().toString(), "")
.replace('\n', ' ').replace(" ", " ").trim();
header = header.replace(" ", " ").trim();
date = date.replace(" ", " ").trim();
text += "\n" + date;
News n = new News(header, text, target, author, date,
News.NEWS_PROJECT);
news.add(n);
}
我的应用程序在(!)
标记的行上冻结,但不会继续。是的我知道String.replace是邪恶的,但我没想到我的应用程序会如此糟糕,因为Android ICS没有滞后。谁能解释一下我发生了什么?
修改
我已使用以下代码替换了(!)
标记的行:
String text0 = list[i].getText().toString();
String text1 = text0.replace(header, "");
String text2 = text1.replace(footer.getText().toString(), "");
String text3 = text2.replace('\n', ' ');
String text4 = text3.replace(" ", " ");
String text5 = text4.trim();
没有任何改变。我的应用程序停留在第一个替换(text1)。
答案 0 :(得分:0)
在Android 2.2中,String.replace()的实现似乎存在一个错误。我找不到Android 2.2的确切源代码,但我设法找到Android 2.1的源代码。 replace方法的实现是:
public String replace(CharSequence target, CharSequence replacement) {
if (target == null) {
throw new NullPointerException("target should not be null");
}
if (replacement == null) {
throw new NullPointerException("replacement should not be null");
}
String ts = target.toString();
int index = indexOf(ts, 0);
if (index == -1)
return this;
String rs = replacement.toString();
StringBuilder buffer = new StringBuilder(count);
int tl = target.length();
int tail = 0;
do {
buffer.append(value, offset + tail, index - tail);
buffer.append(rs);
tail = index + tl;
} while ((index = indexOf(ts, tail)) != -1);
//append trailing chars
buffer.append(value, offset + tail, count - tail);
return buffer.toString();
}
如果target
和replacement
都是空字符串,则do-while循环不会终止。 index
和tail
都初始化为零。 indexOf(ts, tail)
返回tail
的值,该值为零。由于tail = index + tl
和tail
都为零,index
不会增加tl
。
这解释了观察到的行为。我假设由于user1256821在Android 2.2中观察到相同的行为,因此Android 2.2中仍存在此错误。