2016-05-27 14 views
5

Ciao sto utilizzando Volley per la mia pagina di accesso. Ho bisogno di passare i dati come questo modoPost Metodo che utilizza Volley non funziona

{ 
userID : '-988682425884628921', 
email :'[email protected]', 
passwd : '123ss' 
} 

Sto usando il metodo POST per inviare i dati, ho già il check-in DHC, l'API sta lavorando bene in DHC e sto ottenendo JSON Response, ma quando provo con Volley i non sono in grado di ottenere risposta. e nemmeno nessun errore nel mio logcat.

codice Java

RequestQueue mVolleyQueue = Volley.newRequestQueue(this); 

    CustomRequest req = new CustomRequest(Request.Method.POST,url,null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.v("tag","login response " + response); 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.v("tag","login error response " + error.getMessage()); 
     } 
    }){ 
     @Override 
     public Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> params = new HashMap<String, String>(); 

      params.put("userID", "-988682425884628921"); 
      params.put("email", "[email protected]"); 
      params.put("passwd", "123ss"); 
      return params; 
     } 


      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headers = new HashMap<String, String>(); 
       headers.put("Content-Type", "application/json; charset=utf-8"); 
       return headers; 
     } 
    }; 
    mVolleyQueue.add(req); 

errore

05-28 09:20:14.696 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError is ! null 
05-28 09:20:14.697 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError status code : 400 
05-28 09:20:14.697 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError message : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> 
<HTML><HEAD><TITLE>Bad Request</TITLE> 
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> 
<BODY><h2>Bad Request - Invalid Header</h2> 
<hr><p>HTTP Error 400. The request has an invalid header name.</p> 
</BODY></HTML> 


    } 
+0

Provare a utilizzare StringRequest anziché JsonObjectRequest. Vedo che il JSON è in formato JSONArray, quindi questo potrebbe essere il problema. –

+0

stringrequest @SomnathPal anche non funziona –

+0

Si passa parametri nel corpo, è necessario eseguire l'override getparams() e passare i parametri ci – Dory

risposta

4

risolto il problema. Basta parametri utilizzati JsonArrayRequest e passati in JsonObject forma:

Map<String, String> params = new HashMap<String, String>(); 
    params.put("userID", "userid"); 
    params.put("email","email"); 
    params.put("passwd", "password"); 
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, "url", new JSONObject(params), 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        System.out.println("response -->> " + response.toString()); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        System.out.println("change Pass response -->> " + error.toString()); 
       } 
      }); 

    request.setRetryPolicy(new 

      DefaultRetryPolicy(60000, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 


    Volley.newRequestQueue(activity).add(request); 

Non c'è bisogno di rilevante getParams() o getHeaders().

Problema: 1 si stavano diventando codice di risposta 500 perché il server accettava la params come JsonObject e stiamo cercando di alimentare String.

Problema: 2 Si stava utilizzando JsonObjectRequet ma la risposta dal server era in JsonArray quindi è necessario utilizzare JsonArrayRequest ad accettare la risposta in JsonArray

Prova e fammi sapere questo aiuta o no :)

+0

Grande ... Buona codifica .. !! –

1

È necessario eseguire l'override protected Map<String, String> getParams() passare i parametri in POST.

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

 
       @Override 
 
       public void onResponse(JSONObject response) { 
 
        Log.d("Login Response", response.toString()); 
 

 

 
        hidepDialog(); 
 
       } 
 
      }, new Response.ErrorListener() { 
 

 
     @Override 
 
     public void onErrorResponse(VolleyError error) { 
 

 
      System.out.println(error.getStackTrace()); 
 
      VolleyLog.d("ErrorVolley", "Error: " + error.getStackTrace()); 
 
      hidepDialog(); 
 
     } 
 
    }) { 
 

 
      @Override 
 
      protected Map<String, String> getParams() { 
 
       Map<String, String> params = new HashMap<String, String>(); 
 
       params.put("userID", "2")); 
 
       params.put("email","[email protected]"); 
 
       params.put("passwd", "ddddd"); 
 

 
       return params; 
 
      } 
 
     };

+0

lo stesso non cambia: :(( –

+0

se lo cambia con stringrequest mostra BasicNetwork.performRequest: codice di risposta inatteso 500 per –

+0

Ho controllato l'errore 500 e ho scoperto che questo errore potrebbe essere causato dal lato server. code @AdityaVyas –

2

Ho avuto un problema simile. Avevo sovrascritto il Content-Type nel metodo getHeaders:

headers.put("Content-Type", "application/json; charset=UTF-8");

ma Volley si aggiunto un parametro Content-Type, per cui vi erano due di questi parametri. Cancellare la linea aveva risolto il mio problema.

+0

Hai reso la mia giornata, ho lottato da ore, e questo era il motivo :) –

0

Attenzione dell'intestazione del tipo di contenuto. Volley lo gestisce diversamente dalle altre intestazioni e non è mostrato in Mappa <> Intestazioni. Invece di rilevante getHeaders() metodo per impostare tipo di contenuto, si dovrebbe ignorare getBodyContentType() in questo modo:

@Override 
public String getBodyContentType() 
{ 
    return "application/json; charset=utf-8"; 
} 
0

Ho lo stesso consumo problematico di questo:

 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show(); 

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show(); 

       } 
      }) { 

     @Override 
     public Map<String, String> getParams() { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 
      headers.put("UN", UserName); 
      headers.put("PW", PassWord); 
      return headers; 


     } 

    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 

solo bisogno di aggiungere headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); a parms

buona fortuna.

Problemi correlati