Adrian Ivasku Adrian Ivasku - 1 year ago 175
Android Question

How to use Jsoup with Volley?

I have a working example with Jsoup and AsyncTask, and that works fine. I am just not satisfied with the performance. It takes 3-6 seconds to load a simple list page with text and images.

I want to boost the performance somehow... so I have stumbled on volley.

Can anyone explain hot to use volley with jsoup?

I use this to get the doc object that holds the particular URL:

public Document GetDocument(String site) {
Document doc = Jsoup.connect(site).timeout(600000)
.data("query", "Java")

return doc;

I guess I would only analyze the data with jsoup and connect/download with volley? Whene I use Jsoup.connect(site).timeout(600000) I should do that with volley ?

Can anyone write/link a simple example using volley and jsoup?

Answer Source

Can anyone write/link a simple example using volley and jsoup?

Under the hood, Jsoup make use of HttpUrlConnection. This class has known unresolved issues, bugs and performance issues on the Android Platform.

Instead, load the data with Volley first then parse it with Jsoup.

Sample Code:

private static RequestQueue myRequestQueue = null;

public Document GetDocument(String site) throws Exception {   
   final Document[] doc = new Document[1];
   final CountDownLatch cdl = new CountDownLatch(1);

   StringRequest documentRequest = new StringRequest( //
        Request.Method.GET, //
        site, //
        new Response.Listener<String>() {
           public void onResponse(String response) {
               doc[0] = Jsoup.parse(response);
        }, //
        new Response.ErrorListener() {
           public void onErrorResponse(VolleyError error) {
               // Error handling
               System.out.println("Houston we have a problem ... !");
        } //

   if (myRequestQueue == null) {
       myRequestQueue = Volley.newRequestQueue(this);

   // Add the request to the queue...

   // ... and wait for the document.
   // NOTE: Be aware of user experience here. We don't want to freeze the app...

   return doc[0];