【问题标题】:HTMLcleaner stack trace errorHTMLcleaner 堆栈跟踪错误
【发布时间】:2013-01-11 03:12:04
【问题描述】:

我正在处理一个项目,但遇到了问题。我正在尝试使用 html 清理器解析 html,然后使用 xpath 返回一个字符串。如果发现错误(确实如此),我让它返回一个堆栈跟踪。我真的不知道如何根据堆栈跟踪进行调试。这是代码。

package ru.habrahabr.stackparser;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.htmlcleaner.TagNode;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class stackParser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.parse);
    button.setOnClickListener(myListener);
}


private ProgressDialog pd;

private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
        pd = ProgressDialog.show(stackParser.this, "Working...",
                "request to server", true, false);
        new parseSite()
                .execute("http://wiki.teamliquid.net/starcraft2/3_Gate_Robo");
    }
};

private class parseSite extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... arg) {
        String output = new String();
        try {
            htmlHelper hh = new htmlHelper(new URL(arg[0]));
            output = hh.htmlHelper(arg[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output;
    }


    protected void onPostExecute(String output) {
        pd.dismiss();
        TextView view = (TextView) findViewById(R.id.tv1);
        view.setText(output);

    }
}

这是我的 HTML 帮助类

package ru.habrahabr.stackparser;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class htmlHelper {
TagNode rootNode;

public htmlHelper(URL url) {
    // TODO Auto-generated constructor stub
}

public String htmlHelper(String arg) throws IOException, XPatherException
{
    CleanerProperties props = new CleanerProperties();

    // set some properties to non-default values
    props.setTranslateSpecialEntities(true);
    props.setTransResCharsToNCR(true);
    props.setOmitComments(true);

    HtmlCleaner cleaner = new HtmlCleaner(props);
    rootNode = cleaner.clean(arg);

    Object[] nodes = rootNode.evaluateXPath("//h1[@id='firstHeading']");

    String things = nodes.toString();

    return things;
}

UI 和加载栏工作正常,但 TextView 不断返回 [Ljava.lang.Object;@42455a88

非常感谢您对此的帮助...我整天都在努力解决这个问题,但似乎无法弄清楚。谢谢!

【问题讨论】:

    标签: android string xpath android-asynctask htmlcleaner


    【解决方案1】:

    尝试从TagNode 读取文本:

    Object[] nodes = rootNode.evaluateXPath("//h1[@id='firstHeading']");
    if(nodes == null || nodes.length < 1) {
        return "";
    }
    
    TagNode tagnode = (TagNode)nodes[0];
    String things = tagnode.getText();
    

    【讨论】:

    • 谢谢,但是通过上述更改,我单击按钮后 textView 只是空白并且进度条消失了。
    【解决方案2】:

    我想通了。 Xpath 查询引用了 XML 文档中的 DOM 树。因此,为了避免抛出异常,DOM 树需要在清理后进行初始化。然后 Xpath 将工作。

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 1970-01-01
      • 2016-06-21
      • 2017-05-19
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多