• volley 之GsonRequest


    这是之前写的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));
            }
        }
    }
  • 相关阅读:
    web页面与多页应用(布局示例普通文档流)
    web页面与多页应用(一)
    Flutter,webview里面实现上传和下载的功能
    Flutter项目删除了相关的dart文件之后运行flutter run或者 F5编译运行时会报这个错误.... were declared as an inputs, but did not exist. Check the definition of target:kernel_snapshot for errors
    vue项目中,点击输入框的时候,弹出的键盘挡住了输入框,需要把输入框展示在可见区域中,不被遮挡
    在IE浏览器上,min-hheight:unset/line-hight:unset不生效问题解决,把unset换成auto,问题只要时IE浏览器设置unset不生效
    本人修改了,需要把新的AndroidManifest.xml 覆盖原来的,AndroidManifest.xml 覆盖问题
    使用Filter来过滤掉需要排除的数组对象
    深拷贝和浅拷贝
    正则表达式
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/4540122.html
Copyright © 2020-2023  润新知