Volley学习-浅析

Volley

最近有用到,看官网学习了下
官网
https://developer.android.google.cn/training/volley/
1. 在主线程中传递数据 ,所以可以方法中直接操作ui控件
2. 网络请求代码,使用 HttpURLConnection实现的

1.简单请求操作

1.1所需权限

<uses-permission android:name="android.permission.INTERNET" />

1.2简单操作

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);

1.3取消网络请求

  1. Define your tag and add it to your requests.
public  static  final  String TAG =  "MyTag";  
StringRequest stringRequest;  // Assume this exists.  
RequestQueue mRequestQueue;  // Assume this exists.  

// Set the tag on the request.  
stringRequest.setTag(TAG);  

// Add the request to the RequestQueue.  
mRequestQueue.add(stringRequest);
  1. In your activity’s onStop() method, cancel all requests that have this tag.
@Override  
protected  void onStop ()  {  
    super.onStop();  
    if  (mRequestQueue !=  null)  { 
        mRequestQueue.cancelAll(TAG);  
    }  
}

2.设置RequestQueue

2.1网络和缓存两大点

RequestQueue需要两件事来完成它的工作:一个用来执行请求的传输的网络 BasicNetwork,以及一个用来处理缓存的缓存DiskBasedCache。
BasicNetwork代码是HttpURLConnection实现的

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();
String url = "http://www.example.com";
// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>(){
    @Override
    public void onResponse(String response){
        // Do something with the response
    }
},
new Response.ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError error){
        // Handle error
    }
});
// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);
// ...

2.2使用单例

如果你的应用经常使用网络,最好设置一个请求队列的单一实例。
1. 在Application.oncreate()中设置RequestQueue,这种官方不推荐
2. 推荐的方法是实现一个封装RequestQueue和其他功能的单例类
注意:RequestQueue必须用Application context实例化,而不是activity context
以图片显示为例:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue, ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
}

以下是使用案例:

// Get a RequestQueue  
RequestQueue queue =  MySingleton.getInstance(this.getApplicationContext()). getRequestQueue();   
// ...    
// Add a request (in this example, called stringRequest) to your RequestQueue.  
MySingleton.getInstance(this).addToRequestQueue(stringRequest);

3.标准使用

  • StringRequest指定url,结果以string串形式返回
  • JsonObjectRequest and JsonArrayRequest (both subclasses ofJsonRequest). 指定url,结果以json或json array形式返回

原声 访问,返回json对象

String url =  "http://my-json-feed";  

JsonObjectRequest jsonObjectRequest =  new  JsonObjectRequest  (Request.Method.GET, url,  null,  new  Response.Listener<JSONObject>()  {  @Override  public  void onResponse(JSONObject response)  { mTextView.setText("Response: "  + response.toString());  }  
},  new  Response.ErrorListener()  {  @Override  public  void onErrorResponse(VolleyError error)  {  // TODO: Handle error  }  
});  

// Access the RequestQueue through your singleton class.  
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);

4.实现自定义的请求

4.1自定义请求

对于需要实现定制请求的情况,需要做以下工作:
- 继承Requst类, 表示期望返回的类型,详细可以参考StringRequestImageRequest
- 实现抽象方法parseNetworkResponse()和deliverResponse(),下面将对此进行更详细的描述

4.1.1parseNetworkResponse

响应服务器返回,并信息进行对应解析、转换(如字符串、图像或JSON)

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(response.data,HttpHeaderParser.parseCharset(response.headers));
        return Response.success(gson.fromJson(json, clazz),HttpHeaderParser.parseCacheHeaders(response));
    }
    // handle errors
// ...
}

注意:
parseNetworkResponse的参数NetworkResponse包含byte[], HTTP status code, 和响应头
必须返回一个 Response对象,成功时为你需要的响应对象,失败时为失败信息

如果协议中有非标准的缓冲语义,你可以这样做:

return  Response.success(myDecodedObject,HttpHeaderParser.parseCacheHeaders(response));

Volley调用parseNetworkResponse在工作线程中,这个可以确保耗时操作不会阻塞主线程(ui线程)

4.1.2deliverResponse

Volley calls you back on the main thread with the object you returned inparseNetworkResponse(). Most requests invoke a callback interface here, for example:
这块不是太理解,等分析源码了,再完善吧!

protected  void deliverResponse(T response)  { 
    listener.onResponse(response);
}

5. Example: GsonRequest

Gson搞的一个案例

public class GsonRequest<T> extends Request<T> {
    private final Gson gson = new Gson();
    private final Class<T> clazz;
    private final Map<String, String> headers;
    private final Listener<T> listener;

    /**
     * Make a GET request and return a parsed object from JSON.
     *
     * @param url URL of the request to make
     * @param clazz Relevant class object, for Gson's reflection
     * @param headers Map of request headers
     */
    public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
            Listener<T> listener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.clazz = clazz;
        this.headers = headers;
        this.listener = listener;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    }

    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
                    response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(
                    gson.fromJson(json, clazz),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));
        }
    }
}

原理图

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值