Search in sources :

Example 21 with DefaultRetryPolicy

use of com.android.volley.DefaultRetryPolicy in project ShoppingCart by joefei.

the class VollyHelperNew method sendRequestPostOfJSON.

public RequestQueue sendRequestPostOfJSON(String url, JSONObject object, final Handler handler, final int what) {
    JsonObjectRequest json = new JsonObjectRequest(Method.POST, url, object, new Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            checkResponse(response, handler, what);
        }
    }, new ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            sendError(handler, error);
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return TokenUtils.getHeaders(context);
        }
    };
    json.setRetryPolicy(new DefaultRetryPolicy() {

        @Override
        public int getCurrentRetryCount() {
            return 2;
        }
    });
    mQueue.add(json);
    return mQueue;
}
Also used : ErrorListener(com.android.volley.Response.ErrorListener) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 22 with DefaultRetryPolicy

use of com.android.volley.DefaultRetryPolicy in project ShoppingCart by joefei.

the class VollyHelperNew method sendRequest.

/**
 * @param url     请求地址
 * @param map     请求参数列表
 * @param handler 请求界面的Handler
 * @param what    请求界面的上下文
 * @param method  请求方式 POST | PUT | GET
 * @author WuGefei
 */
public RequestQueue sendRequest(String url, HashMap<String, String> map, final Handler handler, final int what, int method) {
    JSONObject jo = null;
    if (method != Method.GET || map != null) {
        jo = new JSONObject(map);
    }
    JsonObjectRequest json = new JsonObjectRequest(method, url, jo, new Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            checkResponse(response, handler, what);
        }
    }, new ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            sendError(handler, error);
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return TokenUtils.getHeaders(context);
        }
    };
    /* 解决重复请求问题 */
    json.setRetryPolicy(new DefaultRetryPolicy(60000, 0, 1f) {

        @Override
        public int getCurrentRetryCount() {
            /*
                 * Volley默认尝试两次,MAX=1,count=0;count<=MAX;count++;count=2时,
				 * 表示当前已经重复请求两次,就不会再第三次重复请求,从而屏蔽掉Volley的自动重复请求功能;
				 */
            return 2;
        }
    });
    mQueue.add(json);
    return mQueue;
}
Also used : ErrorListener(com.android.volley.Response.ErrorListener) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 23 with DefaultRetryPolicy

use of com.android.volley.DefaultRetryPolicy in project EasyVolley by asifmujteba.

the class ASFRequest method start.

public void start() {
    if (mRequestQueueWeakReference.get() != null && getRequest() != null) {
        if (shouldUseCache() && isCachableHTTPMethod() && getCache() != null) {
            ASFCache.ASFEntry entry = getCache().get(getCacheKey());
            if (entry != null) {
                try {
                    getResponseListener().onResponse((T) entry.getData());
                    return;
                } catch (ClassCastException e) {
                }
            }
        }
        if (!shouldUseCache()) {
            getRequest().setShouldCache(false);
        }
        getRequest().setRetryPolicy(new DefaultRetryPolicy(mNetworkTimeoutTime, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        mRequestQueueWeakReference.get().add(getRequest());
        mRequestQueueWeakReference.get().start();
        EasyVolley.addedRequest(this);
    }
}
Also used : DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy)

Aggregations

DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)23 VolleyError (com.android.volley.VolleyError)15 Response (com.android.volley.Response)13 NetworkResponse (com.android.volley.NetworkResponse)10 RequestQueue (com.android.volley.RequestQueue)10 HashMap (java.util.HashMap)10 JSONObject (org.json.JSONObject)10 ExecutionException (java.util.concurrent.ExecutionException)9 TimeoutException (java.util.concurrent.TimeoutException)9 Pair (android.util.Pair)8 StringRequest (com.android.volley.toolbox.StringRequest)8 GsonBuilder (com.google.gson.GsonBuilder)8 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)7 Gson (com.google.gson.Gson)7 JsonRestRequest (com.wordpress.rest.JsonRestRequest)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 JSONException (org.json.JSONException)4 ErrorListener (com.android.volley.Response.ErrorListener)3 RestRequest (com.wordpress.rest.RestRequest)3 ProfileItem (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem)2