2014-06-18 2 views
13

Sto tentando di pubblicare alcuni parametri nella mia API di rails utilizzando Volley in Android. Questo è il codice:Volley non chiama getParams() per la richiesta POST standard

Ho provato con due istruzioni di registro, una in getParams() e un'altra in getHeaders(). Quello in getHeaders() viene registrato mentre l'altro non lo è. Perché la pallavolo ignora 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); 

} 
+1

È abbastanza facile da risolvere, controlla la mia risposta [qui] (http://stackoverflow.com/a/24402403/1294681). – VinceStyling

risposta

26

Utilizzando StringRequest al posto di 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); 
+4

Non posso credere che sia stato questo. Stavo usando un JSONObjectRequest e il debugger non ha mai colpito il metodo getParams(), quindi nessun parametro POST. Nel momento in cui sono passato a StringRequest, il debugger ha colpito getParams(). ¬_¬ –

+1

Grazie mille! Mi hai salvato la giornata –

+3

Grazie, mi hai salvato la giornata. C'è un motivo per cui per JsonObjectRequest il metodo getParams sovraccarico non viene richiamato? – Ajay

1

Il terzo parametro deve essere un JSONObject non è necessario il getparams() metodo giusto passarli nella richiesta.

JsonObjectRequest jsonObjReq = new JsonObjectRequest(
      method, 
      url, 
      jsonObjParams, // <<< HERE 
      responseListener, 
      errorListener); 
0

Per JSONobjectRequest le getparams() non funziona per le richieste POST quindi bisogna fare un customRequest e sovrascrivere getparams() metodo di laggiù. È perché JsonObjectRequest è esteso a JsonRequest che sovrascrive direttamente il metodo getBody(), quindi il tuo getParam() non invocherà mai. Per invocare il metodo getParams() devi prima scavalcare getBody(). O per una soluzione semplice puoi usare StringRequest.

0

è successo perché la cache dei parametri di Volley.

pulita in questo modo

requestQueue.getCache().clear();

Spero che sia utile!