waqas waqas - 1 year ago 75
Java Question

Can I find HTML tags using AsyncHttpResponseHandler or AsyncHttpClient classes?

I am writing a

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()) {
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String body = new String(responseBody);

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


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

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

It is finding
tag in the a string that is the response of a web document using
. Can I find
as generally do by using
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
class? As 4th line is
Elements links = doc.select("h1"); responseMessage = links.text();

Any help or direction will be appreciative.

Answer Source

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

Document doc = Jsoup.parseBodyFragment(body);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download