我可以使用AsyncHttpResponseHandler或AsyncHttpClient类找到HTML标签吗?

时间:2016-08-17 11:16:06

标签: java android android-asynctask android-async-http

我正在Android中编写webcrawler。我的代码是

public void parseHttp()  {
        AsyncHttpClient client = new AsyncHttpClient();
        String url = "http://stackoverflow.com/questions/38959381/unable-to-scrape-data-from-internet-using-android-intents";

        client.get(url, new AsyncHttpResponseHandler(Looper.getMainLooper()) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String body = new String(responseBody);
                System.out.println(body);

                Pattern p = Pattern.compile("<h1(.*)<\\/h1>");
                Matcher m = p.matcher(body);
                Log.d("tag", "success");
                if ( m.find() ) {
                    String match = m.group(1);
                    Log.d("tag", match);
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                Log.d("tag", "failure");
            }
        });
    }

在字符串中找到h1标记,该字符串是使用regex的网络文档的响应。我可以通过使用tag库作为

来查找Jsoup
try {
    Document doc;
    URL = requestString;
    doc = Jsoup.connect(URL).timeout(20 * 1000).userAgent("Chrome").get();
    Elements links = doc.select("h1");
    responseMessage = links.text();
} catch (IOException e) {
    responseMessage = e.getMessage();
}

我可以使用Jsoup课程在AsynsHTTPResponceHandler中找到标签吗?第4行是Elements links = doc.select("h1"); responseMessage = links.text(); 任何帮助或指示都将是值得赞赏的。

1 个答案:

答案 0 :(得分:0)

Jsoup允许从String解析文档,而不是通过HTTP(S)直接加载它。

Document doc = Jsoup.parseBodyFragment(body);