final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest strReq = new StringRequest(Request.Method.GET,
"http://example.com/", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Document document = Jsoup.parse(response);
Elements elementss = document.select("div.category > li");
for (Element element : elements) {
Elements naslov = el.select("div.text > li.headline);
}
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Greška", Toast.LENGTH_SHORT).show();
pDialog.hide();
}
});
AppController.getInstance().addToRequestQueue(strReq);
我怎样才能传递URL参数,而不是每次想要解析数据时都创建新的StringRequest,因为不同的URL具有相同的HTML结构,我可以在将StringRequest添加到RequestQueue时传递URL参数,如:
AppCore.getInstance("http://example.com/").addToRequestQueue(strReq);
多次使用相同的StringRequest,但使用不同的URL?
此外,这是我的AppCore.java:
package app.android.volley;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppCore extends Application {
public static final String TAG = AppCore.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static AppCore mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppCore getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
基本上我尝试做的是多次使用一个StringRequest,但网址不同。
提前致谢。