Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

android - Volley not calling getParams() for standard POST request

I am trying to post some parameters to my rails API using Volley in Android. This is the code:

I tried with two log statements, one in getParams() and another in getHeaders(). The one in getHeaders() is logged while the other one is not. Why is volley ignoring getParams()?

{
//full_name,email,password are private variables defined for this class

String url =  "http://10.0.2.2:3000/users/sign_up.json" ; 

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                 url, null,
                 new Response.Listener<JSONObject>() {

                     @Override
                     public void onResponse(JSONObject response) {
                         Log.d(TAG, response.toString());
                         pDialog.hide();
                     }
                 }, new Response.ErrorListener() {

                     @Override
                     public void onErrorResponse(VolleyError error) {
                         VolleyLog.d(TAG, "Error: " + error.getMessage());
                         pDialog.hide();
                     }
                 }) {

             @Override
             public Map<String, String> getParams() {
                 Map<String, String> params = new HashMap<String, String>();

                 //This does not appear in the log
                 Log.d(TAG,"Does it assign params?") ;


                 params.put("name", full_name.getText().toString());
                 params.put("email",email.getText().toString());
                 params.put("password", password.getText().toString());

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

                 //This appears in the log  
                 Log.d(TAG,"Does it assign headers?") ;

                 HashMap<String, String> headers = new HashMap<String, String>();
                 headers.put("Content-Type", "application/json; charset=utf-8");

                 return headers;
             }

         };

        // Adding request to request queue
        VHelper.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using StringRequest in place of JsonObjectRequest

StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Log.d(TAG, ""+error.getMessage()+","+error.toString());
        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("id", "28");
            params.put("value", "1");

            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;
        }
    };

        AppController.getInstance().addToRequestQueue(sr);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...