2015-11-05 17 views
5

嗨我想發送刪除請求服務器使用齊射和身體參數齊射。但我無法發送請求成功刪除請求使用標題和參數排序

我曾嘗試

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"); 

,這裏是我的VolleyRequest請求類

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

    } 

} 

當我嘗試這種代碼,我得到400響應代碼錯誤請讓我知道,如果任何人都可以幫助我..那我做錯了什麼。謝謝

這裏的屏幕截圖刪除Api我測試和它的工作正常。

I need to send this Data to server

And here is the response form server

+0

嗨!由於我沒有工作憑證(用戶,傳遞),所以我的服務器URL的響應代碼是500,而不是400 :) – BNK

+0

我認爲你可以嘗試'params.put(「Authorization」,「Basic」+ auth);' – BNK

+0

它不能使用params。並且m從應用程序發送請求時得到400,但是當我從瀏覽器打到URL時,它運行良好..我的用戶名在圖像中指定,傳遞爲1234,如果您可以檢查,請檢查並告訴我做錯了什麼。謝謝 –

回答

5

UPDATE:

我已經張貼了我的工作示例項目GitHub修復java.net.ProtocolException: DELETE does not support writing,請看一看。


您的應用程序得到400錯誤代碼,因爲數據體尚未與DELETE請求一起發送。

裏面HurlStack.java,你會發現以下內容:

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

所以我們可以看到DELETE請求忽略身體數據。有一個解決辦法,那就是創建一個CustomHurlStack類(複製上述HurlStack的所有內容),只有修改爲如下:

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

然後,在您的活動,請致電:

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

請請注意,此解決方法僅適用於API21 +(我未測試API20)。從API19-,java.net.ProtocolException: DELETE does not support writing將被拋出。

P/S:如果您的應用程序添加compileSdkVersion 23useLibrary 'org.apache.http.legacy'文件build.gradle內,當創建CustomHurlStack類,你得到錯誤。

希望這有助於!

+0

可以請你告訴我如何創建像這裏newRequestQueue方法http://stackoverflow.com/a/30748487/3593066http://stackoverflow.com/a/30748487/3593066。中號有點困惑,請解釋一下,因爲我得到這個錯誤 java.net.ProtocolException:DELETE不支持寫入 –

+0

如果你有過服務器端的應用程序(Web服務)的控制,我建議你換,這樣DELETE請求使用URL參數而不是的身體參數 – BNK

+0

這是主要問題...我沒有訪問網絡服務 –