使用Volley Android请求删除

时间:2016-04-12 02:09:36

标签: android android-volley

我尝试使用Volley将DELETE请求发送到REST Api,其中包含X-user-email和X-User-token等参数。我已经在互联网上尝试了几乎所有内容,但我仍然收到400错误消息。

当我使用POST请求它的工作时,我认为它对DELETE来说是一样的,我只是改变了:

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

通过

(Request.Method.DELETE, url, params, new Response.Listener<JSONObject>() {...}

我的代码如下:

MainActivity.java

disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {;
                JSONObject params = new JSONObject();
                try {
                    params.put("X-User-Email", "test@test.com");
                    params.put("X-User-Token", "QwerTyuI789IOP");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetworkManager.getInstance(v.getContext()).delete("http://10.0.2.2:3000/users/sign_out", params, new NetworkManager.NetworkManagerListener<String>()  {
                    @Override
                    public void getResult(String response)
                    {
                        if (!response.isEmpty())
                        {
                            result.setText(response.toString());
                            Log.e("Response", response.toString() );
                        }
                    }
                });
            }
        });

NetworkManager.java

public void delete(String url, JSONObject params, final NetworkManagerListener<String> listener) {
        JsonObjectRequest request = new JsonObjectRequest
                (Request.Method.DELETE, url, params, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG + ": ", "delete onResponse : " + response.toString());
                        if (null != response.toString())
                            listener.getResult(response.toString());
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (null != error.networkResponse) {
                            Log.d(TAG + ": ", "delete Error Response code: " + error.networkResponse.statusCode);
                            listener.getResult(error.toString());
                        }
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Accept", "application/json");
                params.put("Content-type", "application/json");
                return params;
            }
        };
        ;
        requestQueue.add(request);
    }

你知道如何解决这个问题吗?谢谢你读我。

1 个答案:

答案 0 :(得分:5)

由于 DELETE 请求会忽略正文数据,因此您应该创建一个CustomHurlStack类。

请在以下SO问题中阅读更多我的回答:

  

Delete Request With header and Parametes Volley

或尝试my GitHub sample project。希望它有所帮助!