如何使用Jsoup用排枪? [英] How to use Jsoup with Volley?

查看:226
本文介绍了如何使用Jsoup用排枪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Jsoup和AsyncTask的一个工作示例,并且工作正常。我只是不满意的表现。它需要3-6秒文本和图像来加载一个简单的列表页。

我想以某种方式提高性能,所以,我无意中发现了凌空。

谁能解释热利用与凌空jsoup?

我使用它来获取保存在特定的URL的doc对象:

 公开文件GetDocument(字符串网站){
        文档的DOC = Jsoup.connect(网站).timeout(600000)
        。数据(查询,Java的)
        .userAgent(Mozilla的)
        。得到();        返回文档;
 }

我想我只会对数据进行分析与jsoup和凌空连接/下载? Whene我用Jsoup.connect(网站).timeout(600000)我应该做的,与凌空?

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


解决方案

  

任何人都可以写/使用抽射链接一个简单的例子,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

这篇关于如何使用Jsoup用排枪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆