Skip to content Skip to sidebar Skip to footer

Unable To Scrape Data From Internet Using Android Intents

I am unable to scrape heading from a web page using intents in Android. For the time being, I just want to extract heading text (h1 tag text) from a URL. I wrote a piece of code fo

Solution 1:

Have you considered using an http framework for Android instead? It's a lot less code and also runs the requests in the background. This example uses the loopj async client

build.gradle:

compile'com.loopj.android:android-async-http:1.4.9'compile'cz.msebera.android:httpclient:4.4.1.2'

Test code:

@TestpublicvoidparseHttp()throws Exception {

    AsyncHttpClientclient=newAsyncHttpClient();
    finalCountDownLatchlatch=newCountDownLatch(1);

    Stringurl="http://stackoverflow.com/questions/38959381/unable-to-scrape-data-from-internet-using-android-intents";

    client.get(url, newAsyncHttpResponseHandler(Looper.getMainLooper()) {
        @OverridepublicvoidonSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            Stringbody=newString(responseBody);
            Patternp= Pattern.compile("<h1(.*)<\\/h1>");
            Matcherm= p.matcher(body);
            Log.d("tag", "success");
            if ( m.find() ) {
                Stringmatch= m.group(1);
                Log.d("tag", match);
            }
            latch.countDown();
        }

        @OverridepublicvoidonFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

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

    latch.await();
}

Update:

Added additional import above: compile 'cz.msebera.android:httpclient:4.4.1.2'

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpRequest;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.HttpStatus;

Post a Comment for "Unable To Scrape Data From Internet Using Android Intents"