Skip to content
This repository was archived by the owner on Feb 26, 2018. It is now read-only.
This repository was archived by the owner on Feb 26, 2018. It is now read-only.

v1.0.17 JSONObjectRequest and the debugger never hit the getParams method. #82

Closed
@chenghaojun

Description

@chenghaojun

This is request inner class extends from JsonObjectRequest and I override the getParams method to set custom POST params.

    private class ImageUploadRequest extends JsonObjectRequest {
        public ImageUploadRequest(int method, String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, listener, errorListener);
        }

        @Override
        public Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("cover[base64]", imageThumbnailBase64Str);
            params.put("cover[name]", null);
            params.put("ref_g", "Wap");
            params.put("ref_m", "Platform");
            params.put("ref_a", "register");
            params.put("access_token", Constants.ACCESS_TOKEN_UPLOAD_IMAGE);

            Log.v(TAG, params.toString());

            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers = new HashMap<String, String>();
            headers.put("Content-Type","application/x-www-form-urlencoded");
            headers.put("abc", "value");
            return headers;
        }

    }

and this is the code post the request to server.

        ImageUploadRequest request = new ImageUploadRequest(
                1,
                String.format("%s/index.php?g=Wap&m=Upload&a=server&token=%s&is_ngdata=1",
                        Constants.SERVER_URL, Constants.TOKEN),
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        // status: 0 失败,status: 1 成功
                        // error 错误消息
                        Log.v(TAG, response.toString());
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });

        WeiPingTaiApplication.getInstance().getRequestQueue().add(request);

but the getParams never been triggered.

Activity

mcxiaoke

mcxiaoke commented on Aug 3, 2015

@mcxiaoke
Owner

@chenghaojun JsonRequest is for using JSONObject as request body and response body, it ignores the params and uses body from mRequestBody you pass in the constructor, so you should extends Request and create custom Request subclass. see here: JsonRequest.java

chenghaojun

chenghaojun commented on Aug 3, 2015

@chenghaojun
Author

Thanks.

I passed my params in the constructor, and the request posted to server as expected. but the request Content-Typs is application/json, my server can't resolve json data.

I override the getHeaders() method, change the Content-Type from application/json to application/x-www-form-urlencoded, the server correctly handle my request.

e6cd154b-3f64-4fce-8a6a-88e333dc3d12

code:
060c4925-d8ce-45f2-a987-c9751a83edf0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mcxiaoke@chenghaojun

        Issue actions

          v1.0.17 JSONObjectRequest and the debugger never hit the getParams method. · Issue #82 · mcxiaoke/android-volley