使用库内凌空库 [英] Using volley library inside a library
问题描述
我想打一个库,以减少重复我的网络运行的每一个Android项目甚至放弃我的罐子一些其他开发者使用网络通讯我的方法。
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.
所以我建立这个:
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).
如何使这样一个本?构建有有给参数,并把它在特定URL的方法的类的JAR库,备有凌空库?
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?
推荐答案
定义界面像
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);
}
和
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.
这篇关于使用库内凌空库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!