gpt4 book ai didi

java - Android 2.2 中的 String.replace 性能问题

转载 作者:行者123 更新时间:2023-12-01 04:56:31 25 4
gpt4 key购买 nike

我正在编写一个 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("&nbsp;", " ").trim();
header = header.replace("&nbsp;", " ").trim();
date = date.replace("&nbsp;", " ").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("&nbsp;", " ");
String text5 = text4.trim();

什么都没有改变。我的应用程序在第一次替换 (text1) 时卡住了。

最佳答案

Android 2.2 中 String.replace() 的实现似乎存在错误。我找不到 Android 2.2 的确切源代码,但我设法找到了 Android 2.1 的源代码。替换方法的实现是:

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();
}

如果targetreplacement都是空字符串,do-while循环不会终止。 indextail 都初始化为零。 indexOf(ts, tail) 返回 tail 的值,该值为零。 tail = index + tl 不会增加 tail,因为 indextl 都为零。

这解释了观察到的行为。我假设由于 user1256821 在 Android 2.2 中观察到了相同的行为,因此该错误在 Android 2.2 中仍然存在。

关于java - Android 2.2 中的 String.replace 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com