2015-11-05 17 views
5

Ciao Voglio inviare Elimina Richiesta al server utilizzando Volley lungo Intestazioni e parametri del corpo. ma io non sono in grado di inviare richiesta con successoElimina richiesta con intestazione e Parameti Volley

quello che ho cercato

JSONObject jsonbObjj = new JSONObject(); 
try { 
    jsonbObjj.put("nombre", Integer.parseInt(no_of_addition 
      .getText().toString())); 
    jsonbObjj.put("cru", crue); 
    jsonbObjj.put("annee", 2010); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
VolleyRequest mVolleyRequest = new VolleyRequest(
     Method.DELETE, url, jsonbObjj, 

     new Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject jsonObject) { 
       // TODO Auto-generated method stub 

       if (pDialog != null) { 
        pDialog.dismiss(); 
       } 
       Log.e("Server Response", "response = " 
         + jsonObject.toString()); 
      } 

     }, new ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError arg0) { 
       // TODO Auto-generated method stub 
       if (pDialog != null) { 
        pDialog.dismiss(); 
       } 
       Log.e("Error Response", 
         "Error " + arg0.getMessage()); 
       Log.e("Error Response", 
         "Error = " + arg0.getCause()); 

      } 
     }, mUserSession.getUserEmail(), mUserSession 
       .getUserPassword(), false); 

ApplicationController.getInstance().addToRequestQueue(
     mVolleyRequest, "deleteRequest"); 

Ed ecco la mia VolleyRequest classe richiesta

public class VolleyRequest extends JsonObjectRequest { 

    String email, pass; 
    boolean saveCookeis; 

    public VolleyRequest(int method, String url, JSONObject jsonRequest, 
      Listener<JSONObject> listener, ErrorListener errorListener, 
      String email, String pass, boolean saveCookie) { 
     super(method, url, jsonRequest, listener, errorListener); 
     // TODO Auto-generated constructor stub 
     this.email = email; 
     this.pass = pass; 
     this.saveCookeis = saveCookie; 
    } 

    public VolleyRequest(int method, String url, JSONObject jsonRequest, 
      Listener<JSONObject> listener, ErrorListener errorListener) { 
     super(Method.POST, url, jsonRequest, listener, errorListener); 
     // TODO Auto-generated constructor stub 

    } 

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     // TODO Auto-generated method stub 
      HashMap<String, String> params = new HashMap<String, String>(); 

      String auth = ""; 
      try { 
       auth = android.util.Base64.encodeToString(
         (this.email + ":" + this.pass).getBytes("UTF-8"), 
         android.util.Base64.DEFAULT); 
      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      params.put("Authorization", auth); 
      return params; 
    } 

    @Override 
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
     // TODO Auto-generated method stub 

     if (saveCookeis) { 
      try { 
       String jsonString = new String(response.data, 
         HttpHeaderParser.parseCharset(response.headers)); 

       ApplicationController.getInstance().checkSessionCookie(
         response.headers); 

       return Response.success(new JSONObject(jsonString), 
         HttpHeaderParser.parseCacheHeaders(response)); 

      } catch (UnsupportedEncodingException e) { 
       return Response.error(new ParseError(e)); 
      } catch (JSONException je) { 
       return Response.error(new ParseError(je)); 
      } 
     } 
     return super.parseNetworkResponse(response); 

    } 

} 

Quando ho provato questo codice ottengo 400 codice di risposta errore Per favore fatemi sapere se qualcuno può aiutarmi .. che cosa sto facendo male. Grazie

qui le schermate per Elimina Api che ho testato e funziona bene.

I need to send this Data to server

And here is the response form server

+0

Ciao! Dal momento che non ho credenziali di lavoro (utente, pass), quindi il codice di risposta che ho ricevuto con l'url del tuo server è 500, non 400 :) – BNK

+0

Penso che puoi provare con 'params.put (" Autorizzazione "," Base "+ auth); ' – BNK

+0

non funziona con i parametri. e sto ricevendo 400 mentre invio richiesta dall'applicazione ma funziona bene quando colpisco l'URL dal browser .. il mio nome utente è specificato nell'immagine e il passaggio è 1234 se puoi controllare per favore controllalo e dimmi cosa sto facendo male. Grazie –

risposta

5

UPDATE:

Ho inviato il mio progetto di esempio di lavoro per GitHub per risolvere java.net.ProtocolException: DELETE does not support writing, si prega di dare un'occhiata.


La vostra applicazione ha ottenuto 400 codice di errore, perché il corpo dei dati non è stato inviato con DELETE richiesta.

All'interno HurlStack.java, troverete la seguente:

  case Method.DELETE: 
       connection.setRequestMethod("DELETE"); 
       break; 
      case Method.POST: 
       connection.setRequestMethod("POST"); 
       addBodyIfExists(connection, request); 
       break; 

modo che possiamo vedere DELETE richiesta ignora i dati del corpo. C'è una soluzione, cioè si crea una classe CustomHurlStack (copiare tutto il contenuto della HurlStack sopra), con la sola modifica come segue:

  case Request.Method.DELETE: 
       connection.setRequestMethod("DELETE"); 
       addBodyIfExists(connection, request); 
       break; 

Poi, nella sua attività, lo chiamano:

CustomHurlStack customHurlStack = new CustomHurlStack(); 
RequestQueue queue = Volley.newRequestQueue(this, customHurlStack); 

prega nota che questa soluzione funziona solo per API21 + (API20 non ho testato). Da API19-, verrà generato il numero java.net.ProtocolException: DELETE does not support writing.

P/S: aggiungi useLibrary 'org.apache.http.legacy' all'interno del vostro file di build.gradle se la vostra applicazione compileSdkVersion 23 e si ottiene errore quando creare CustomHurlStack classe.

Spero che questo aiuti!

+0

puoi dire come posso creare il metodo newRequestQueue come qui http://stackoverflow.com/a/30748487/3593066http://stackoverflow.com/a/30748487/3593066. m poco confuso per favore spiegami perché sto ricevendo questo errore java.net.ProtocolException: DELETE non supporta la scrittura –

+0

Se hai il controllo sull'app lato server (servizio web), ti suggerisco di cambiare in modo che la richiesta DELETE usi invece i parametri URL di body params – BNK

+0

questo è il problema principale ... non ho accesso al servizio web –

Problemi correlati