Java Code Examples for com.android.volley.toolbox.JsonObjectRequest#setRetryPolicy()

The following examples show how to use com.android.volley.toolbox.JsonObjectRequest#setRetryPolicy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StoryService.java    From hex with Apache License 2.0 6 votes vote down vote up
public Story getStory(String storyId) {
    String STORY_PATH = "/story";
    String storyPath = mApiBaseUrl + STORY_PATH + "/" + storyId;

    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    JsonObjectRequest request = new JsonObjectRequest(storyPath, future, future);
    request.setRetryPolicy(RetryPolicyFactory.build());
    mRequestQueue.add(request);

    try {
        JSONObject response = future.get();
        return StoryMarshaller.marshall(response);
    } catch (Exception e) {
        return null;
    }
}
 
Example 2
Source File: MainActivity.java    From ms-identity-android-native with MIT License 4 votes vote down vote up
private void callGraphAPI() {
    Log.d(TAG, "Starting volley request to graph");

    /* Make sure we have a token to send to graph */
    if (authResult.getAccessToken() == null) {return;}

    RequestQueue queue = Volley.newRequestQueue(this);
    JSONObject parameters = new JSONObject();

    try {
        parameters.put("key", "value");
    } catch (Exception e) {
        Log.d(TAG, "Failed to put parameters: " + e.toString());
    }
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, MSGRAPH_URL,
            parameters,new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            /* Successfully called graph, process data and send to UI */
            Log.d(TAG, "Response: " + response.toString());

            updateGraphUI(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error: " + error.toString());
        }
    }) {
        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + authResult.getAccessToken());
            return headers;
        }
    };

    Log.d(TAG, "Adding HTTP GET to Queue, Request: " + request.toString());

    request.setRetryPolicy(new DefaultRetryPolicy(
            3000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(request);
}
 
Example 3
Source File: BaseNetwork.java    From Airbnb-Android-Google-Map-View with MIT License 4 votes vote down vote up
/**
 * For Post Method with parameters in the content body
 *
 * @param methodType   Type of network call eg, GET,POST, etc.
 * @param url          The url to hit
 * @param paramsObject JsonObject for POST request, null for GET request
 * @param requestType  Type of Network request
 */
public void getJSONObjectForRequest(int methodType, String url, JSONObject paramsObject, final int requestType) {
    if (NetworkUtil.isInternetConnected(mContext)) {
        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (methodType, url, paramsObject, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            handleResponse(response, requestType);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        handleError(error);
                    }
                }) {

            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=UTF-8";
            }


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return getRequestHeaderForAuthorization();
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }
        };
        int socketTimeout = 5000;//30 seconds - change to what you want
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        jsObjRequest.setRetryPolicy(policy);
        CustomVolleyRequestQueue.getInstance(mContext).getRequestQueue().add(jsObjRequest);
    }
}