Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

privatestaticRequestQueuemyRequestQueue=null;

public Document GetDocument(String site)throws Exception {   
   final Document[] doc = newDocument[1];
   finalCountDownLatchcdl=newCountDownLatch(1);
 
   StringRequestdocumentRequest=newStringRequest( //
        Request.Method.GET, //
        site, //newResponse.Listener<String>() {
           @OverridepublicvoidonResponse(String response) {
               doc[0] = Jsoup.parse(response);
               cdl.countDown();
           }
        }, //newResponse.ErrorListener() {
           @OverridepublicvoidonErrorResponse(VolleyError error) {
               // Error handling
               System.out.println("Houston we have a problem ... !");
               error.printStackTrace();
           }
        } //
   );

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

   // Add the request to the queue...
   myRequestQueue.add(documentRequest);

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

   return doc[0];
}

References

Solution 2:

With Stephan`s answer I have made some little modifications to this code and it looks like this. I have added UTF 8 support so it can read other languages and specified the retry policy.

privatestaticRequestQueuemyRequestQueue=null;

public Document GetDocument(String site) {
    final Document[] doc = newDocument[1];
    finalCountDownLatchcdl=newCountDownLatch(1);
    try {

        StringRequestdocumentRequest=newStringRequest( //
                Request.Method.GET, //
                site, //newResponse.Listener<String>() {
                    @OverridepublicvoidonResponse(String response) {
                        StringnewStr=null;
                        try {
                            newStr = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"), "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        doc[0] = Jsoup.parse(newStr);
                        cdl.countDown();
                    }
                }, //newResponse.ErrorListener() {
                    @OverridepublicvoidonErrorResponse(VolleyError error) {
                        // Error handling
                        System.out.println("Houston we have a problem ... !");
                        error.printStackTrace();
                    }
                } //
        );

        if (myRequestQueue == null) {
            myRequestQueue = Volley.newRequestQueue(MainActivity._Instance);

            documentRequest.setRetryPolicy(newDefaultRetryPolicy(5000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        }

        // Add the request to the queue...
        myRequestQueue.add(documentRequest);

        // ... and wait for the document.// NOTA: Be aware of user experience here. We don't want to freeze the app...
        cdl.await();
    }
    catch (Exception e) {
        Log.d("TMS", "Error parsing page " + site);
        e.printStackTrace();
        returnnull;
    }
    return doc[0];
}

Solution 3:

Add Volley to your project.

implementation 'com.android.volley:volley:1.2.1'

Then use the code below,

 runOnUiThread(() -> {
 // Stuff that updates the UInewThread(() -> {
     
     finalStringBuilderbuilder1=newStringBuilder();
     
     Stringurl="your.url.com";                                   //RequestQueue initializedRequestQueuemRequestQueue= Volley.newRequestQueue(this);     //String Request initializedStringRequestmStringRequest=newStringRequest(Request.Method.GET, url, response -> {
     
     //Toast.makeText(getApplicationContext(),"Response :" + response.toString(), Toast.LENGTH_LONG).show();Documentdoc= Jsoup.parse(response);
     
     Elementstrs= doc.select("table.content tr");
     
     for (Element tr : trs) 
         {
             Elementstds= tr.getElementsByTag("td");
             Elementtd= tds.get(0);
             builder1.append(td.text()).append(",");
         }
         
         Stringstr1= builder1.toString();
         
         runOnUiThread(() -> {
             String[] items1 = str1.split(",");
             inti=1;
             while (i < items1.length) 
             {
                 //
                 p++;
             }
             });
             }, error -> Log.i(TAG,"Error :" + error.toString()));
            mRequestQueue.add(mStringRequest);
     }).start();
});

Post a Comment for "How To Use Jsoup With Volley?"