zoukankan      html  css  js  c++  java
  • Volley封装

    Volley.jar下载

    在Application初始化 Volley queues=Volley.newRequestQueue(appContext); 

    并返回RequestQueue 对象

    public static RequestQueue getHttpQueues(){
    return queues;
    }

    import android.content.Context;
    
    import com.android.volley.Response.ErrorListener;
    import com.android.volley.Response.Listener;
    import com.android.volley.VolleyError;
    importpublic abstract class VolleyInterface {
        public Context context;
        public static Listener<ResultModel> mListener;
        public static ErrorListener mErrorListener;
        
        public VolleyInterface(Context context,Listener<ResultModel> lisener,ErrorListener errorLisener){
            this.context=context;
            mListener=lisener;
            mErrorListener=errorLisener;
        }
        
        public Listener<ResultModel> loadingListener(){
            mListener=new Listener<ResultModel>() {
    
                @Override
                public void onResponse(ResultModel resultModel) { 
                    onMySuccess(resultModel);
                }
            };
            return mListener;
        }
        
        public ErrorListener errorListener(){
            mErrorListener=new ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    OnMyError(volleyError);
                }
            };
            return mErrorListener;
        }
        
        public abstract void onMySuccess(ResultModel resultModel);
        public abstract void OnMyError(VolleyError volleyError);
        
    }
    View Code

    VolleyRequest类

    import java.util.Map;
    
    import android.content.Context;
    
    import com.android.volley.Request.Method;
    importimportpublic class VolleyRequest {
        private static FastJsonRequest<ResultModel> fastJsonRequest;
        public static Context context;
        public static void RequestGet(Context mContext,String url,String tag,VolleyInterface vif){
            FandDaApplication.getHttpQueues().cancelAll(tag);
            fastJsonRequest=new FastJsonRequest<ResultModel>(url, ResultModel.class, vif.loadingListener(), vif.errorListener()); 
            fastJsonRequest.setTag(tag);
            FandDaApplication.getHttpQueues().add(fastJsonRequest);
            //调用会引发com.android.volley.NoConnectionError: java.io.InterruptedIOException错误,原因是volley已经调用过了
            //FandDaApplication.getHttpQueues().start();
        }
        
        
        public static void RequestPost(Context context,String url,Map<String,String> map,String tag,VolleyInterface vif){
            FandDaApplication.getHttpQueues().cancelAll(tag);
            fastJsonRequest=new FastJsonRequest<ResultModel>(Method.POST,url, ResultModel.class, map,vif.loadingListener(), vif.errorListener()); 
            fastJsonRequest.setTag(tag);
            FandDaApplication.getHttpQueues().add(fastJsonRequest);
            //调用会引发com.android.volley.NoConnectionError: java.io.InterruptedIOException错误,原因是volley已经调用过了
            //FandDaApplication.getHttpQueues().start(); 
        }
        
        
    }
    View Code

     使用方式

    String url=new BroadcastAPI().getBroadcastList(0,"",0,"",page,pageSize);
                    VolleyRequest.RequestGet(context, url, "NEWINVILIST", 
                            new VolleyInterface(context,VolleyInterface.mListener,VolleyInterface.mErrorListener) { 
                        @Override
                        public void onMySuccess(ResultModel resultModel) { 
                             if(ResultApi.isCode(resultModel)){
                                 list =JSON.parseArray(resultModel.getResult().toString(), BroadcastDetailsModel.class);
                                    //如果用户刷新数据则清空原来缓存记录,为了保证数据统一性
                                    BroadcastTable.getInstance(context).deleteAllData(BroadcastTable.newNumber);
                                    if (mType == 1) { 
                                        adapter.addItemTop(list);
                                    } else {
                                        adapter.addItemLast(list);
                                    }
                                    BroadcastTable.getInstance(context).saveBroadcastList(list,BroadcastTable.newNumber);
                             }else{ 
                                    if(mType==1){
                                        ToastUtil.show(context, getString(R.string.toast_empty_data));
                                    }else{
                                        ToastUtil.show(context, getString(R.string.toast_next_empty));
                                    } 
                                } 
                             mSwipeRefreshWidget.setRefreshing(false);
                        }
                        
                        @Override
                        public void OnMyError(VolleyError volleyError) {
                            ToastUtil.show(context, volleyError.getMessage());
                             mSwipeRefreshWidget.setRefreshing(false);
                        }
                    });
    View Code
    public class ResultModel {
        private int code;
        private String msg;
        private Object result;
        public int getCode() {
            return code;
        }
        public void setCode(int code) {
            this.code = code;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
        public Object getResult() {
            return result;
        }
        public void setResult(Object result) {
            this.result = result;
        } 
    View Code
  • 相关阅读:
    maven命令
    一个Maven项目在eclipse中正常,但在IDEA中启动时报错
    idea导入eclipse的普通web项目
    idea启动慢的解决办法
    如何在idea里切换项目不同分支
    定时任务
    springboot项目打成war包
    springboot配置信息
    Controller层方法传参和返回字符串
    [C#学习] popupControlContainer控件
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/5075413.html
Copyright © 2011-2022 走看看