码迷,mamicode.com
首页 > 其他好文 > 详细

volley 之GsonRequest

时间:2015-05-30 12:05:25      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

这是之前写的http://www.cnblogs.com/freexiaoyu/p/3955137.html

关于GsonReques的用户,这个在POST请求传参数的时候GsonRequest构造第四个参数(POST请求参数)的时候接收不到参数原因是原来的类没有重写一个写法

@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}

用原来的方法想要实现POST参数请求只能在Response.ErrorListener后面重写getParams,下面请求方法中构造为null的就是传递params

GsonRequest<ResultModel> gsonRequest=new GsonRequest<ResultModel>
                (Request.Method.POST, AppConfig.APIURL, ResultModel.class, null, new Response.Listener<ResultModel>() {

                    @Override
                    public void onResponse(ResultModel response) {
                        VolleyLog.e("Error: %s", "成功"+ response); 
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                          VolleyLog.e("Error: %s", "失败"+ error); 
                    }
                }){ 

                    @Override 
                    protected Map<String, String> getParams() 
                            throws AuthFailureError { 
                        Map<String, String> params = new HashMap<String, String>(); 
                        //Post参数
                        return params; 
                    }};
                    requestQueue.add(gsonRequest); 

 

 

下面是一个完成的类

 

import java.io.UnsupportedEncodingException;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;


public class GsonRequest<T> extends Request<T> {  
    private Gson mGson = new Gson();
    private Class<T> clazz;
    private Map<String, String> headers;
    private Map<String, String> params;
    private 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
     */
    public GsonRequest(int method,
                       String url,
                       Class<T> clazz,
                       Listener<T> listener,
                       ErrorListener errorListener) {
        super(method, url, errorListener);
        this.clazz = clazz;
        this.listener = listener;
        mGson = new Gson();
    }

    /**
     * Make a POST 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
     */
    public GsonRequest(int method,
                       String url,
                       Class<T> clazz,
                       Map<String, String> params,
                       Listener<T> listener,
                       ErrorListener errorListener) {

        super(method, url, errorListener);
        this.clazz = clazz;
        this.params = params;
        this.listener = listener;
        this.headers = null;
        mGson = new Gson();
    }

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

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

    @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(
                    mGson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));
        }
    }
}

 

volley 之GsonRequest

标签:

原文地址:http://www.cnblogs.com/freexiaoyu/p/4540122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!