javaandroidandroid-asynctaskandroid-async-http

Can I find HTML tags using AsyncHttpResponseHandler or AsyncHttpClient classes?


I am writing a webcrawler in Android. My code is

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");
            }
        });
    }

It is finding h1 tag in the a string that is the response of a web document using regex. Can I find tag as generally do by using Jsoup library as

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();
}

Can I find tags as in Jsoup using AsynsHTTPResponceHandler class? As 4th line is Elements links = doc.select("h1"); responseMessage = links.text(); Any help or direction will be appreciative.


Solution

  • Jsoup allows to parse the document from a String rather than directly loading it via HTTP(S).

    Document doc = Jsoup.parseBodyFragment(body);