android – 如何使用Jsoup与Volley?

我有一个使用Jsoup和AsyncTask的工作示例,并且工作正常.我对表现不满意.加载带有文本和图像的简单列表页面需要3-6秒.

我想以某种方式提升表现……所以我偶然发现了凌空抽射.

任何人都可以解释热与jsoup使用齐射?

我用它来获取包含特定URL的doc对象:

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

        return doc;
 }

我想我只会用jsoup和连接/下载与volley分析数据?我使用Jsoup.connect(site).timeout(600000)我应该用齐射吗?

任何人都可以使用volley和jsoup编写/链接一个简单的例子吗?

解决方法

Can anyone write/link a simple example using volley and jsoup?

在引擎盖下,Jsoup使用HttpUrlConnection.此类已知Android平台上尚未解决的问题,错误和性能问题.

相反,首先使用Volley加载数据,然后使用Jsoup解析它.

示例代码:

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

参考

> An Introduction to Volley
> Transmitting Network Data Using Volley

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...