Volley

简介:

Volley - Android HTTP client

Part 1 - Quickstart

Were can I get it?

Download volley library and import it as a library project or make a jar file.

git clone https://android.googlesource.com/platform/frameworks/volley

Why Volley?

  • Simple

  • Powerful

  • Extendable

  • Built-in memory cache

  • Built-in disk cache

How to use it?

Step 1 - Create request queue

RequestQueue requestQueue = Volley.newRequestQueue(context.getApplicationContext());

Step 2 - Create request

StringRequest request = new StringRequest(
            Request.Method.GET,
            url,
            listener,
            errorListener);

Step 3 - Create listeners

Response.Listener<String> listener = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        L.d("Success Response: " + response.toString());
    }};Response.ErrorListener errorListener = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        if (error.networkResponse != null) {
            L.d("Error Response code: " +  error.networkResponse.statusCode);
        }
    }};

Step 4 - Add request to queue

requestQueue.add(request);

Request methods:

  • Request.Method.GET

  • Request.Method.POST

  • Request.Method.PUT

  • Request.Method.DELETE

Request types:

Volley request diagram

Every request listener returns appropriate type.

  • String

  • Json Object

  • Json Array

  • Bitmap

You can create your own type

Example of request which adds some cookie.

public class CookieRequest extends StringRequest {

    private String mCookieValue;

        public CookieRequest(String url, String cookieValue,
                Response.Listener<String> listener,
                Response.ErrorListener errorListener) {
            super(Method.GET, url, listener, errorListener);
            mCookieValue = cookieValue;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put("Cookie", mCookieValue);
            return map;
        }}

How to pass post request parameters?

You need to override getParams() method.

StringRequest request = new StringRequest(
        Request.Method.POST,
        url,
        listener,
        errorListener) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "Jon Doe");
        map.put("age", "21");

        return map;
    }};

How to set request retry policy?

StringRequest request = new StringRequest(
        Request.Method.GET,
        url,
        listener,
        errorListener);request.setRetryPolicy(
    new DefaultRetryPolicy(
            DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, // 2500
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 1
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); //1f

HTTP basic authorization

StringRequest request = new StringRequest(
        Request.Method.GET,
        url,
        listener,
        errorListener) {

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return createBasicAuthHeader("user", "passwd");
    }};
Map<String, String> createBasicAuthHeader(String username, String password) {
    Map<String, String> headerMap = new HashMap<String, String>();

    String credentials = username + ":" + password;
    String base64EncodedCredentials =
            Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
    headerMap.put("Authorization", "Basic " + base64EncodedCredentials);

    return headerMap;}

How to cancel request?

StringRequest request1 = new StringRequest(...);request1.setTag("weather-screen"); // request tagStringRequest request2 = new StringRequest(...);request2.setTag("weather-screen"); // request tagrequestQueue.add(request1);requestQueue.add(request2);

To cancel request you just need to remember request tag and call cancelAll(...) method.

requestQueue.cancelAll("weather-screen"); // cancel all requests with "weather-screen" tag

Original article.

SEE ALSO:










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/1661624,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
JSON Java Android开发
Android 中使用Volley进行网络请求和图片加载详解
Android 中使用Volley进行网络请求和图片加载详解
197 0
Android 中使用Volley进行网络请求和图片加载详解
|
10月前
Volley源码分析(一)
Volley源码分析(一)
131 0
|
10月前
|
Android开发
Android Retrofit,Gson,Okhttp混淆
Android Retrofit,Gson,Okhttp混淆
309 0
|
缓存 Java
浅谈Volley请求
浅谈Volley请求
119 0
浅谈Volley请求
关于volley的使用
https://blog.csdn.net/qwm8777411/article/details/45770979
692 0
|
缓存
Volley源码解析
Volley是一款轻量级的网络访问框架,适合小批量的数据传输。Volley的使用通过newRequestQueue创建一个RequestQueue对象,并调用RequestQueue.add方法来提交任务。
1037 0