android jsoup android-volley

android - ¿Cómo usar Jsoup con Volley?



android-volley (2)

Tengo un ejemplo de trabajo con Jsoup y AsyncTask, y eso funciona bien. Simplemente no estoy satisfecho con el rendimiento. Lleva de 3 a 6 segundos cargar una página de lista simple con texto e imágenes.

Quiero aumentar el rendimiento de alguna manera ... así que me topé con la volea.

¿Alguien puede explicar hot para usar volley con jsoup?

Lo uso para obtener el objeto doc que contiene la URL particular:

public Document GetDocument(String site) { Document doc = Jsoup.connect(site).timeout(600000) .data("query", "Java") .userAgent("Mozilla") .get(); return doc; }

¿Supongo que solo analizaría los datos con jsoup y me conectaría / descargaría con volley? Cuando uso Jsoup.connect (site) .timeout (600000) ¿debería hacerlo con voley?

¿Alguien puede escribir / vincular un ejemplo simple usando volley y jsoup?


¿Alguien puede escribir / vincular un ejemplo simple usando volley y jsoup?

Debajo del capó, Jsoup hace uso de HttpUrlConnection . Esta clase ha conocido problemas no resueltos, errores y problemas de rendimiento en la plataforma Android.

En su lugar, cargue los datos con Volley primero y luego analícelos con Jsoup.

Código de muestra:

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]; }

Referencias


Con la respuesta de Stephan, he hecho algunas pequeñas modificaciones a este código y se ve así. He agregado compatibilidad con UTF 8 para que pueda leer otros idiomas y especifique la política de reintento.

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]; }