简体   繁体   English

使用Volley(Android)发送带有参数的REST Api的DELETE请求?

[英]Send a DELETE Request using Volley (Android) to a REST Api with parameters?

How do I send a DELETE Request using Volley (Android) to a REST Api with parameters like access_token and username. 如何使用Volley(Android)向REST Api发送带有access_token和username等参数的DELETE请求。 I have tried almost everything on Internet but still I am getting a 400 error Message. 我在互联网上尝试了几乎所有的东西,但我仍然收到400错误消息。 I have tried sending the same DELETE request using PostMan and it works perfectly fine. 我尝试使用PostMan发送相同的DELETE请求,它完全正常。

Here is the Error: E/Volley﹕ [1112] BasicNetwork.performRequest: Unexpected response code 400 for http://myserverip/messages/2 这是错误: E / Volley:[1112] BasicNetwork.performRequest: http:// myserverip / messages / 2的意外响应代码400

and Here is the Java code that I am using: 这是我正在使用的Java代码:

String URI = "http://myserverip/messages/2";

JsonObjectRequest request = new  JsonObjectRequest(Request.Method.DELETE,URI,null,
                                    new Response.Listener<JSONObject>() {
                                        @Override
                                        public void onResponse(JSONObject response) {

Toast.makeText(contextm,response.toString(),Toast.LENGTH_SHORT).show();

                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {


                                        }


                                    }){

                                @Override
                                public Map<String, String> getHeaders() throws AuthFailureError {
                                    Map<String, String> headers = super.getHeaders();

                                    if (headers == null
                                            || headers.equals(Collections.emptyMap())) {
                                        headers = new HashMap<String, String>();
                                    }

                                    headers.put("access_token", "access_token");
                                    headers.put("username", "username");


                                    return headers;
                                }
                            };

                            MyApp.getInstance().addToRequestQueue(request);

Might not be really useful but here is the working DELETE request preview from PostMan 可能不是很有用,但这里是PostMan正在进行的DELETE请求预览

  • DELETE /messages/1 HTTP/1.1 DELETE / messages / 1 HTTP / 1.1
  • Host: serveripaddress 主机:serveripaddress
  • Cache-Control:no-cache 缓存控制:无缓存
  • Content-Type: application/x-www-form-urlencoded 内容类型:application / x-www-form-urlencoded

    access_token=SPAVYaJ7ea7LzdeQYrBTsIRssuGbVpJI8G9XSV0n&username=whit3hawks =的access_token&SPAVYaJ7ea7LzdeQYrBTsIRssuGbVpJI8G9XSV0n用户名= whit3hawks

You can easily achieve this if you put the following instead of null in ... new JsonObjectRequest(Request.Method.DELETE,URI,null,new Response.Listener<JSONObject>() { ... 如果在... new JsonObjectRequest(Request.Method.DELETE,URI,null,new Response.Listener<JSONObject>() { ...以下内容而不是null ,则可以轻松实现此目的... new JsonObjectRequest(Request.Method.DELETE,URI,null,new Response.Listener<JSONObject>() { ...

final JSONObject object = new JSONObject();

try {
    object.put("access_token", "SPAVYaJ7ea7LzdeQYrBTsIRssuGbVpJI8G9XSV0n");
    object.put("username", "whit3hawks");
} catch (Exception e) {
    e.printStackTrace();
}

So your Request should look like this: ... new JsonObjectRequest(Request.Method.DELETE,URI,object,new Response.Listener<JSONObject>() { ... 所以你的Request应该是这样的: ... new JsonObjectRequest(Request.Method.DELETE,URI,object,new Response.Listener<JSONObject>() { ...

You posted a long time ago, but maybe someone else will need this some time. 你很久以前发布过,但也许其他人需要这个时间。

I am successfully sending headers in volley while using DELETE with this code: 我使用此代码使用DELETE时成功发送了标题:

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

just tried it and works like a charm. 只是试了一下,就像一个魅力。

EDIT: 编辑:

if you're using a Mac, try checking with Charles what exactly it is you are sending, on a PC you can try fiddler. 如果您使用的是Mac,请尝试查看Charles究竟发送的是什么,在PC上您可以尝试使用fiddler。

also, error 400 means 'Bad Input', so the server is either getting more info then it should, or less then he expects. 此外,错误400意味着'输入错误',因此服务器要么获得更多信息,要么应该获得更多信息,或者更少,然后他预期。 and keep in mind that DELETE in Volley acts as a GET, make sure you are sending an empty body with the request. 并记住,排球中的DELETE充当GET,请确保您发送一个带有请求的空体。

hope that helped somehow.. happy coding 希望以某种方式帮助...快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM