如何在 Volley 中使用 Jsoup? [英] How to use Jsoup with Volley?
问题描述
我有一个 Jsoup 和 AsyncTask 的工作示例,效果很好.我只是对性能不满意.加载带有文本和图像的简单列表页面需要 3-6 秒.
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.
谁能解释一下如何在 jsoup 中使用 volley?
Can anyone explain hot to use volley with jsoup?
我用它来获取包含特定 URL 的 doc 对象:
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;
}
我想我只会用 jsoup 分析数据并用 volley 连接/下载?当我使用 Jsoup.connect(site).timeout(600000) 我应该用 volley 来做吗?
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 ?
任何人都可以使用 volley 和 jsoup 编写/链接一个简单的示例吗?
Can anyone write/link a simple example using volley and jsoup?
推荐答案
任何人都可以使用 volley 和 jsoup 编写/链接一个简单的示例吗?
Can anyone write/link a simple example using volley and jsoup?
在底层,Jsoup 使用了 HttpUrlConnection
.此类在 Android 平台上存在已知未解决的问题、错误和性能问题.
Under the hood, Jsoup make use of HttpUrlConnection
. This class has known unresolved issues, bugs and performance issues on the Android Platform.
相反,先用 Volley 加载数据,然后用 Jsoup 解析它.
Instead, load the data with Volley first then parse it with 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];
}
参考资料
- Volley 简介莉>
- 使用 Volley 传输网络数据
这篇关于如何在 Volley 中使用 Jsoup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!