且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Jsoup用排枪?

更新时间:2023-12-03 21:56:46


  

任何人都可以写/使用抽射链接一个简单的例子,jsoup?


块引用>

引擎盖下,Jsoup利用 HttpURLConnection类的。这个类有已知未解决的问题,漏洞和性能问题Android平台上。

相反,装入抽射数据第一,然后用Jsoup解析它。

样品code

 私有静态请求队列myRequestQueue = NULL;公开文件GetDocument(字符串网站)抛出异常{
   最后文件[] = DOC新的文档[1];
   最终CountDownLatch CDL =新CountDownLatch(1);   StringRequest documentRequest =新StringRequest(//
        Request.Method.GET,//
        现场,//
        新Response.Listener<串GT;(){
           @覆盖
           公共无效onResponse(字符串响应){
               DOC [0] = Jsoup.parse(响应);
               cdl.coutDown();
           }
        } //
        新Response.ErrorListener(){
           @覆盖
           公共无效onErrorResponse(VolleyError错误){
               //错误处理
               的System.out.println(休斯敦,我们有一个问题...!);
               error.printStackTrace();
           }
        } //
   );   如果(myRequestQueue == NULL){
       myRequestQueue = Volley.newRequestQueue(本);
   }   //添加请求到队列...
   myRequestQueue.add(documentRequest);   // ...并等待该文档。
   // NOTA:请注意这里的用户体验。我们不希望冻结的应用程序...
   cdl.await();   返回文档[0];
}

参考

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")
        .userAgent("Mozilla")
        .get();

        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?

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.coutDown();
           }
        }, //
        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.
   // NOTA: Be aware of user experience here. We don't want to freeze the app...
   cdl.await();

   return doc[0];
}

References