本文整理了Java中com.android.volley.RequestQueue
类的一些代码示例,展示了RequestQueue
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RequestQueue
类的具体详情如下:
包路径:com.android.volley.RequestQueue
类名称:RequestQueue
[英]A request dispatch queue with a thread pool of dispatchers. Calling #add(com.android.volley.Request) will enqueue the given Request for dispatch, resolving from either cache or network on a worker thread, and then delivering a parsed response on the main thread.
[中]一个包含调度程序线程池的请求调度队列。调用#add(com.android.volley.Request)会将给定的请求排队等待分派,在工作线程上从缓存或网络解析,然后在主线程上传递解析后的响应。
代码示例来源:origin: stackoverflow.com
HurlStack hurlStack = new HurlStack() {
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
public void onResponse(JSONObject response) {
try {
mTextView.setText(response.toString(5));
} catch (JSONException e) {
mTextView.setText(e.toString());
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText(error.toString());
requestQueue.add(jsonObjectRequest);
代码示例来源:origin: mcxiaoke/android-volley
@Test public void cancelAll_onlyCorrectTag() throws Exception {
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
Object tagA = new Object();
Object tagB = new Object();
Request req1 = mock(Request.class);
when(req1.getTag()).thenReturn(tagA);
Request req2 = mock(Request.class);
when(req2.getTag()).thenReturn(tagB);
Request req3 = mock(Request.class);
when(req3.getTag()).thenReturn(tagA);
Request req4 = mock(Request.class);
when(req4.getTag()).thenReturn(tagA);
queue.add(req1); // A
queue.add(req2); // B
queue.add(req3); // A
queue.cancelAll(tagA);
queue.add(req4); // A
verify(req1).cancel(); // A cancelled
verify(req3).cancel(); // A cancelled
verify(req2, never()).cancel(); // B not cancelled
verify(req4, never()).cancel(); // A added after cancel not cancelled
}
}
代码示例来源:origin: mcxiaoke/android-volley
/**
* Verify RequestFinishedListeners are informed when requests are successfully delivered
*
* Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
*/
@Test public void add_requestFinishedListenerSuccess() throws Exception {
NetworkResponse response = mock(NetworkResponse.class);
Request request = new MockRequest();
RequestFinishedListener listener = mock(RequestFinishedListener.class);
RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
queue.addRequestFinishedListener(listener);
queue.addRequestFinishedListener(listener2);
queue.start();
queue.add(request);
verify(listener, timeout(100)).onRequestFinished(request);
verify(listener2, timeout(100)).onRequestFinished(request);
queue.stop();
}
代码示例来源:origin: chentao0707/SimplifyReader
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link android.content.Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
代码示例来源:origin: stackoverflow.com
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
代码示例来源:origin: jiangqqlmj/FastDev4Android
requestQueue.add(imageRequest);
break;
case R.id.btn_image_loader:
tv_result.setVisibility(View.GONE);
img_result.setVisibility(View.VISIBLE);
img_result_network.setVisibility(View.GONE);
requestQueue.add(post_stringRequest);
break;
case R.id.btn_loader_list:
requestQueue.add(gsonRequest);
break;
case R.id.btn_fdv_get_params:
代码示例来源:origin: stackoverflow.com
jsonParams.put("param1", param1);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
new Response.Listener<JSONObject>()
requestQueue.add(request);
代码示例来源:origin: stackoverflow.com
JSONObject obj = new JSONObject();
obj.put("id", "1");
obj.put("name", "myname");
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
hideProgressDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
});
queue.add(jsObjRequest);
代码示例来源:origin: stackoverflow.com
requestBody = new JSONObject();
requestBody.put("A", new JSONArray(new String[]{ "123", "456", "789" });
requestBody.put("B", "23-11-2016");
requestBody.put("C", "ABC");
} catch (JSONException exception) {
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonRequest.setRetryPolicy(policy);
mQueue.add(jsonRequest);
代码示例来源:origin: InnoFang/Android-Code-Demos
private String volleyPostJsonObjectRequest() {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("phone", "13429667914");
hashMap.put("key", Constant.JUHE_API_KEY);
JSONObject object = new JSONObject(hashMap);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, Constant.JUHE_URL_POST, object,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
request.setTag(JSON_OBJECT_POST_TAG);
MyApplication.getHttpQueues().add(request);
return request.getTag().toString();
}
代码示例来源:origin: stackoverflow.com
try {
String j = response.getString("rows");
JSONArray rowa = new JSONArray(j);
JSONObject rowobj = rowa.getJSONObject(0);
JSONArray elementsa = rowobj.getJSONArray("elements");
JSONObject currentObject = elementsa.getJSONObject(i);
String duration = currentObject.getString("duration_in_traffic");
JSONObject timeObject = new JSONObject(duration);
eta = timeObject.getString("text"); //eta has the needed value**
methodThatUsesValue(eta);
queue.add(jsObjRequest);
代码示例来源:origin: stackoverflow.com
JSONArray jsonArray = new JSONArray(response);
getCountry(jsonArray);
} catch (JSONException e) {
requestQueue.add(stringRequest);
代码示例来源:origin: stackoverflow.com
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com";
StringRequest postRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
queue.add(postRequest);
代码示例来源:origin: commonsguy/cw-omnibus
void enqueue(Request<?> request) {
queue.add(request);
}
代码示例来源:origin: InnoFang/Android-Code-Demos
public static void RequestGet(Context context, String url, String tag, VolleyInterface volleyInterface) {
MyApplication.getHttpQueues().cancelAll(tag); // 先清空请求队列,防止出现重复的请求消耗内存
sStringRequest = new StringRequest(Request.Method.GET, url, volleyInterface.loadingListener(), volleyInterface.errorListener());
sStringRequest.setTag(tag);
MyApplication.getHttpQueues().add(sStringRequest);
MyApplication.getHttpQueues().start();
}
public static void RequestPost(Context context, String url, String tag, Map<String, String> params, VolleyInterface volleyInterface) {
代码示例来源:origin: mcxiaoke/android-volley
/**
* Cancels all requests in this queue with the given tag. Tag must be non-null
* and equality is by identity.
*/
public void cancelAll(final Object tag) {
if (tag == null) {
throw new IllegalArgumentException("Cannot cancelAll with a null tag");
}
cancelAll(new RequestFilter() {
@Override
public boolean apply(Request<?> request) {
return request.getTag() == tag;
}
});
}
代码示例来源:origin: stackoverflow.com
String url = "http://stackoverflow.com";
StringRequest request = new StringRequest(url, new Listener<String>() {
@Override
public void onResponse(String response) {
// handle response
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle error
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
request.setTag(TAG); // you can add tag to request
requestQueue.add(request); // add request to queue
requestQueue.cancelAll(TAG); // you can cancel request using tag
代码示例来源:origin: stackoverflow.com
RequestQueue mRequestQueue;
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);
// Start the queue
mRequestQueue.start();
代码示例来源:origin: bumptech/glide
@After
public void tearDown() throws IOException {
mockWebServer.shutdown();
requestQueue.stop();
}
代码示例来源:origin: stackoverflow.com
public static RequestQueue newRequestQueueForTest(final Context context, final OkHttpClient okHttpClient) {
final File cacheDir = new File(context.getCacheDir(), "volley");
final Network network = new BasicNetwork(new OkHttpStack(okHttpClient));
final ResponseDelivery responseDelivery = new ExecutorDelivery(Executors.newSingleThreadExecutor());
final RequestQueue queue =
new RequestQueue(
new DiskBasedCache(cacheDir),
network,
4,
responseDelivery);
queue.start();
return queue;
}
内容来源于网络,如有侵权,请联系作者删除!