Using volley library inside a library

天大地大妈咪最大 提交于 2019-12-11 23:37:49

问题


I want to make a library to reduce my duplicate network works on every android projects or even give my jar to some other developers to using my methods for network communications.

So i build this:

import java.util.Map;
import org.json.JSONObject;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;

public class RequestResp {
    private final static String WEB_SERVICE_URL = "http://blabla/api";
    private final Priority priorityImmediatelly = Priority.IMMEDIATE;
    private final Priority priorityHigh = Priority.HIGH;
    private final Priority priorityNORMAL = Priority.NORMAL;
    private String tag_req_default = "tag_req_default";
    VolleyCustomRequest mVolleyCustomReq;
    DefaultRetryPolicy drp = new DefaultRetryPolicy(15000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

    public /*JSONObject*/ void sendParamsAsHighPriority(Map<String, String> params) {

        mVolleyCustomReq = new VolleyCustomRequest(Method.POST,
                WEB_SERVICE_URL, params, new Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null) {
                        }
                    }
                }, new ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(tag_req_default, error.getMessage());
                    }
                }) {

            @Override
            public Priority getPriority() {
                return priorityHigh;
            }

        };
        mVolleyCustomReq.setRetryPolicy(drp);
        VolleyController.getInstance().addToRequestQueue(mVolleyCustomReq,
                tag_req_default);
/*return response; ?!?!?*/
    }
}

But how to return response?! Cause if server was busy or down or something that make response a little late, developers in their applications get null!(i guess).

How to make a such this?! Build a jar library that has a class that has a method that give parameters and send it on specific URL, with volley library?


回答1:


Define Interface like

public interface OntaskCompleted {

    public void onSuccess(JSONObject response);
    public void onError(String message);
}

Now Your activity should implement this interface and you have to override these method.

Now in you Volley class do this.

 if (response != null) {
 ontaskCompleted.onSuccess(JSONObject);
                        }

and

 public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(tag_req_default, error.getMessage());
                        ontaskCompleted.onError( error.getMessage());
                                                }

Now your activity will get the result of error or success. Hope it helps you.



来源:https://stackoverflow.com/questions/30092261/using-volley-library-inside-a-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!