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.
我想建立一个库,以减少我在每个Android项目上的重复网络工作,甚至让我的jar给其他开发人员使用我的方法进行网络通信。
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?
怎么做这样的?!构建一个jar库,它有一个类,该类有一个方法可以提供参数并通过volley库在特定的URL上发送它?
1 个解决方案
#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.
现在你在Volley课上做到这一点。
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.
现在您的活动将得到错误或成功的结果。希望它能帮到你。
#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.
现在你在Volley课上做到这一点。
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.
现在您的活动将得到错误或成功的结果。希望它能帮到你。