Android volley to handle redirect

前端 未结 8 1257
名媛妹妹
名媛妹妹 2020-11-27 18:43

I recently started to use Volley lib from Google for my network requests. One of my requests get error 301 for redirect, so my question is that can volley handle redirect so

8条回答
  •  盖世英雄少女心
    2020-11-27 19:24

    I am using volley:1.1.1 with https url though the request was having some issue. On digging deeper i found that my request method was getting changed from POST to GET due to redirect (permanent redirect 301). I am using using nginx and in server block i was having a rewrite rule that was causing the issue.

    So in short everything seems good with latest version of volley. My utility function here-

    public void makePostRequest(String url, JSONObject body, final AjaxCallback ajaxCallback) {
        try {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                    url, body, new Response.Listener() {
    
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(LOG, response.toString());
                    ajaxCallback.onSuccess(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(LOG, error.toString());
                    ajaxCallback.onError(error);
                }
            });
            singleton.getRequestQueue().add(jsonObjectRequest);
        } catch(Exception e) {
            Log.d(LOG, "Exception makePostRequest");
            e.printStackTrace();
        }
    }
    
    // separate file
    public interface AjaxCallback {
        void onSuccess(JSONObject response);
        void onError(VolleyError error);
    }
    

提交回复
热议问题