1

我正在编写一个加载 HTML 页面并从中获取特定数据的 android 应用程序。我已经使用 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) 处。

4

1 回答 1

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 循环不会终止。两者indextail都初始化为零。indexOf(ts, tail)返回 的值tail,该值为零。tail = index + tl不会增加tail,因为indextl都为零。

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

于 2012-12-27T15:39:26.650 回答