How to make separate class for volley library and call all method of volley from another activity and get response?

前端 未结 5 1490
不知归路
不知归路 2020-12-01 06:42

how to create a separate class in which define all about volley and in another activity we directly pass URL,CONTEXT and Get Response...

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 07:13

    public class VolleyService {
    
        IResult mResultCallback = null;
        Context mContext;
    
        VolleyService(IResult resultCallback, Context context)
        {
            mResultCallback = resultCallback;
            mContext = context;
        }
    
        //--Post-Api---
        public void postDataVolley(String url,final Map param){
            try {
                StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener() {
                    @Override
                    public void onResponse(String response) {
                        if(mResultCallback != null)
                            mResultCallback.notifySuccessPost(response);
                    }
                },  new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if(mResultCallback != null)
                            mResultCallback.notifyError(error);
                    }
                }) {
                    @Override
                    protected Map getParams() {
                        return param;
                    }
    
                    @Override
                    public Map getHeaders() throws AuthFailureError {
                        Map params = new HashMap();
                        params.put("Content-Type", "application/x-www-form-urlencoded");
                        return params;
                    }
                };
                AppController.getInstance(mContext).addToRequestQueue(sr);
    
            }catch(Exception e){
    
            }
        }
        //==Patch-Api==
        public void patchDataVolley(String url,final HashMap param)
        {
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.PATCH, url, new JSONObject(param),
                    new Response.Listener() {
                        @Override
                        public void onResponse(JSONObject response) {
                            if(mResultCallback != null)
                                mResultCallback.notifySuccessPatch(response);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            if(mResultCallback != null)
                                mResultCallback.notifyError(error);
                        }
                    }) {
                @Override
                public Map getHeaders() throws AuthFailureError {
                    HashMap headers = new HashMap();
                    return headers;
                }
            };
            AppController.getInstance(mContext).addToRequestQueue(request);
        }
    }
    
    public interface IResult {
        void notifySuccessPost(String response);
    
        void notifySuccessPatch(JSONObject jsonObject);
    
        void notifyError(VolleyError error);
    
    }
    

提交回复
热议问题