How to use Jsoup with Volley?

后端 未结 2 1253
盖世英雄少女心
盖世英雄少女心 2020-11-29 12:38

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 te

相关标签:
2条回答
  • 2020-11-29 13:31

    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.

    private static RequestQueue myRequestQueue = null;
    
    public Document GetDocument(String site) {
        final Document[] doc = new Document[1];
        final CountDownLatch cdl = new CountDownLatch(1);
        try {
    
            StringRequest documentRequest = new StringRequest( //
                    Request.Method.GET, //
                    site, //
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            String newStr = null;
                            try {
                                newStr = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            doc[0] = Jsoup.parse(newStr);
                            cdl.countDown();
                        }
                    }, //
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // Error handling
                            System.out.println("Houston we have a problem ... !");
                            error.printStackTrace();
                        }
                    } //
            );
    
            if (myRequestQueue == null) {
                myRequestQueue = Volley.newRequestQueue(MainActivity._Instance);
    
                documentRequest.setRetryPolicy(new DefaultRetryPolicy(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();
            return null;
        }
        return doc[0];
    }
    
    0 讨论(0)
  • 2020-11-29 13:34

    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>() {
               @Override
               public void onResponse(String response) {
                   doc[0] = Jsoup.parse(response);
                   cdl.countDown();
               }
            }, //
            new Response.ErrorListener() {
               @Override
               public void onErrorResponse(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

    • An Introduction to Volley
    • Transmitting Network Data Using Volley
    0 讨论(0)
提交回复
热议问题