Android用JSoup解析网页

1、不可以在UI线程直接读取网络数据,所以另起线程处理这件事情

public class MainActivity extends ActionBarActivity implements IParseJsonCallback,IParseHtmlCallback{

    public void ParseHtmlPage(List<PhaseResultBean> resultList) {
        new ParseHtml(this).execute(resultList);
    }

    @Override
    public void ParseHtmlDone(List<PhaseResultBean> resultList) {
        //回调函数
    }
}

2、回调接口

package com.neohope.android.gpjj;

import java.util.List;

public interface IParseHtmlCallback {
    void ParseHtmlDone(List<PhaseResultBean> resultList);
}

3、解析HTML类

package com.neohope.android.gpjj;

import android.os.AsyncTask;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;

public class ParseHtml extends AsyncTask<List<PhaseResultBean>,Void,List<PhaseResultBean>>{

    private IParseHtmlCallback htmlCallback;

    public ParseHtml(IParseHtmlCallback htmlCallback)
    {
        this.htmlCallback = htmlCallback;
    }

    @Override
    protected List<PhaseResultBean> doInBackground(List<PhaseResultBean>... paramList) {
        if(paramList==null)return null;
        List<PhaseResultBean> params = paramList[0];
        for(PhaseResultBean bean : params) {
            Document doc = null;
            String url = bean.httpUrl;
            try {
                doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").timeout(5000).followRedirects(true).get();
            } catch (IOException ex) {
                Log.w("", ex.getMessage());
                return null;
            }

            Document content = Jsoup.parse(doc.toString());
            Element dodydiv = content.getElementById("bodydiv");
            Element div = dodydiv.child(6).child(1).child(0).child(0);

            Element code = div.child(0).child(2);
            String szCode = code.text();

            Element title = div.child(0).child(1);
            String szTitle = title.text();

            bean.code = szCode;
            bean.name = szTitle;
        }

        return params;
    }

    @Override
    protected void onPostExecute(List<PhaseResultBean> listResult) {
        super.onPostExecute(listResult);
        htmlCallback.ParseHtmlDone(listResult);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}

Comments are closed.