1

我正在尝试使用 HTML 清洁器来解析来自网站的信息,然后使用 Xpath 来查找我正在寻找的数据。我在一个单独的 AsyncTask 类中有 htmlcleaner 的东西,该应用程序似乎可以在我的手机上运行。但是,当我按下按钮时,什么也没有发生。这是我的主要活动类和我的 AsyncTask 类。

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 OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {

        new parseSite().execute("http://xjaphx.wordpress.com/");
    }
};

private class parseSite extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... arg) {
        String output = new String();
        try {
            htmlHelper hh = new htmlHelper();
        } finally {
        }

        return output;
    }

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

这是我引用的课程。如果有人能看到这个并告诉我发生了什么,我将不胜感激。我试图遵循一个工作示例并将我自己的 Url 和 Xpath 放入,但它不起作用。

package ru.habrahabr.stackparser;

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

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

public class htmlHelper {
TagNode rootNode;
String stats;

static final String XPATH_STATS = "//div[@id='blog-stats']/ul/li";


public String htmlHelper(URL htmlPage) throws IOException, XPatherException {
    HtmlCleaner cleaner = new HtmlCleaner();
    rootNode = cleaner.clean(htmlPage);

    // query XPath
    Object[] statsNode = rootNode.evaluateXPath(XPATH_STATS);
    // process data if found any node
    if (statsNode.length > 0) {
        TagNode resultNode = (TagNode) statsNode[0];
        stats = resultNode.getText().toString();
    }
    return stats;
}
}
4

2 回答 2

2

AsyncTask将 doInBackground 方法更改为:

@Override
protected String doInBackground(String... arg) {
        String output "";
        try {
            htmlHelper hh = new htmlHelper();
            output=hh.htmlHelper(arg[0]);  //<< call htmlHelper method here
        } finally {
        }

        return output;
    }

因为您目前只创建一个类的实例,htmlHelper而不是从类调用htmlHelper方法来 htmlHelper从 web url 获取数据

于 2013-01-10T18:26:52.787 回答
1

Look , I will not care of the data is downloaded or not , but I will assume that the data is already downloaded and besides in the variable output , you just need to place this code inside onPostExecute

 runOnUiThread(new Runnable() {

 public void run() {

// update data into TextView.

  TextView view = (TextView) findViewById(R.id.tv1);

view.setText((CharSequence) output);

                    }
                });

because , sometimes the data is already downloaded but needed to be assigned into Textview or whatever view by using UIthread. Hope this works for you

于 2013-01-10T18:30:33.613 回答