mcxiaoke / android-volley

DEPRECATED
4.29k stars 1.56k forks source link

Volley StringRequest getParams() does not call #142

Open hrsalehi opened 8 years ago

hrsalehi commented 8 years ago

I'm trying to use StringRequest with volley and surprisingly the getParams() method never called in POST request (sometimes called but most time not called). In my other projects It work fine but I cant figure It out what is the matter with this one!

Below is my code :

StringRequest request = new StringRequest(Request.Method.POST, SystemConfigs.getNewPosts(context), new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        boolean result = Boolean.parseBoolean(response);
        if (result) {
            publishProgressState(3);
            if (postCount > currentDownloaded) {
                getNewPosts(postsList);
            } else {
                publishProgressState(5);
                downloaded = 0;
                currentDownloaded = 0;
                ds.openReadable();
                String list = ds.getPostsString();
                ds.close();
                getUpdatedPosts(list);

            }
        } else {
            publishProgressState(-1);
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        publishProgressState(-1);
    }
}) {

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            boolean result = savePosts(jsonString, true);
            return Response.success(result + "", HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected Map<String, String> getParams() {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("page", (++downloaded) + "");
        headers.put("list", list == null ? "" : list);

        return headers;
    }

};
request.setRetryPolicy(new

DefaultRetryPolicy(5000,
                   DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                   DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)

);

appSingleton.getRequestQueue().add(request);
hrsalehi commented 8 years ago

Accidently I found out that If I clear the volley cache getParams() method called. But what If I want to call a paged service like above and I want to send page numbers by post request?

chineizo commented 8 years ago

I'm experiencing the same issue. Do you think clearing the getParam() map is the solution to this situation? I tried to use JsonArrayRequest that has JSONArray for the post parameters but my webservice is not able to extract the parameters. So I want to go back to using StringRequest instead and clear the getParams cache like you hinted