本文對Volley簡單封裝,本文隻是記錄volley的使用
主要請求内部實作加載進度條:
資料回調的class
public abstract class CallBack {
protected void onFail(Context context,int errCode,String errMsg) {
}
public abstract void onSuccess(JSONObject json) throws JSONException;
protected void onCancel(){
//TODO 請求取消處理
}
//傳回鍵是否能取消請求
protected boolean canCancel(){
return true;
}
}
單例請求class:
public class HttpUtils {
private static HttpUtils ourInstance = new HttpUtils();
private RequestQueue requestQueue = Volley.newRequestQueue(App.app);
private ProgressDialog dialog;
private Object tag;
public static HttpUtils getInstance() {
return ourInstance;
}
private HttpUtils() {
}
//根據需求添加重載方法
public void Post( String url, final Map<String,String> map, final CallBack callBack) {
makeRequest(Request.Method.POST,url,map,callBack,null,null,false);
}
private void makeRequest(int method, String url, final Map<String,String> map,
final CallBack callBack, final Object tag, Context context, boolean showLoad){
final StringRequest request = new StringRequest(method, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
if (callBack != null) {
int code = object.getInt("code");
if (code == ) {
callBack.onSuccess(object);
}else
callBack.onFail(null,code,object.getString("errMsg"));
}
} catch (JSONException e) {
callBack.onFail(null,-,"讀取伺服器資料失敗");
}
dismissIfNeed();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (callBack!=null) {
callBack.onFail(null, -, "請求伺服器失敗");
}
dismissIfNeed();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
if (map != null)
return map;
return super.getParams();
}
};
request.setRetryPolicy(new DefaultRetryPolicy(, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
if (tag!=null) {
request.setTag(tag);
}
requestQueue.add(request);
dialog = null;
if (showLoad && context!=null && tag!=null) {
dialog = new ProgressDialog(context);
dialog.setCancelable(false);
dialog.show();
this.tag = tag;
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP) {
if (callBack != null && callBack.canCancel()) {
callBack.onCancel();
cancelRequest(tag);
dismissIfNeed();
}
}
return false;
}
});
}
}
private void dismissIfNeed(){
if (dialog!=null&&dialog.isShowing())
dialog.dismiss();
}
public void cancelRequest(Object tag){
requestQueue.cancelAll(tag);
if (equals(this.tag,tag))
dismissIfNeed();
}
public static boolean equals(Object a, Object b) {
return (a == null) ? (b == null) : a.equals(b);
}
public static final class RequestBuilder{
private int method = Request.Method.POST;
private String url;
private Map<String,String> map;
private CallBack callBack;
private Object tag;
//-----------
private Context context;
private boolean showLoad;
public void build(){
if (method == Request.Method.GET && map!=null){
url=url+"?";
for (String key:map.keySet()) {
try {
url=url+key+"="+ URLEncoder.encode(map.get(key),"UTF-8")+"&";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
url = url.substring(, url.length()-);
}
HttpUtils.getInstance().makeRequest(method,url,map,callBack,tag,context,showLoad);
}
public RequestBuilder bindMethod(int method){
this.method = method;
return this;
}
public RequestBuilder bindUrl(String url){
this.url = url;
return this;
}
public RequestBuilder bindMap(Map<String,String> map){
this.map = map;
return this;
}
public RequestBuilder bindCallBack( CallBack callBack){
this.callBack = callBack;
return this;
}
public RequestBuilder bindTag( Object tag){
this.tag = tag;
return this;
}
public RequestBuilder bindContext(Context context){
this.context = context;
return this;
}
public RequestBuilder bindShow(boolean showLoad){
this.showLoad = showLoad;
return this;
}
}
}
使用方式:
new HttpUtils.RequestBuilder().bindUrl("url")
.bindMap(new HashMap<>())
.bindShow(true)
.bindContext(this)
.bindTag("TAST")
.bindCallBack(new CallBack() {
@Override
public void onSuccess(JSONObject json) throws JSONException {
Log.e("TEST",json.toString());
}
@Override
protected void onCancel() {
Log.e("TEST","onCancel--->");
}
}).build();
//---------------或者下面這個方式-------------
HttpUtils.getInstance().Post("url", new HashMap<String, String>(), new CallBack() {
@Override
public void onSuccess(JSONObject json) throws JSONException {
}
});