谷歌凌空忽略POST参数参数、POST

由网友(关键我有胸器)分享简介:我目前正试图通过谷歌排球发送一个简单的POST请求到我的服务器。因此,我已经写了code以下行:I'm currently trying to send a simple POST-request via Google Volley to my server.Therefore I've written the...

我目前正试图通过谷歌排球发送一个简单的POST请求到我的服务器。 因此,我已经写了code以下行:

I'm currently trying to send a simple POST-request via Google Volley to my server. Therefore I've written the following lines of code:

Map<String, String> params = new HashMap<String, String>();
params.put("regId", "skdjasjdaljdlksajskl");
JSONObject object = new JSONObject(params);
JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                "address_of_my_server/method", object,
                successListener, errorListener);
queue.add(request);

不过,我得到一个错误500返回,它说,有一个缺少的参数(REGID)。我试过同一个GET请求,但我得到了相同的结果。

But I get an Error 500 returned, which says, that there is a missing parameter (regId). I've tried the same with a GET-Request, but I got the same result.

只有当我使用StringRequest包含address_of_my_server /法?REGID = sadlasjdlasdklsj200服务器回复一个格式化的URL。

Only when I'm using a StringRequest with a formatted URL like "address_of_my_server/method?regId=sadlasjdlasdklsj" the server replies with 200.

我得到了完全相同的结果,当我使用StringRequest,如:

I get the exact same result when I use a StringRequest like:

StringRequest request = new StringRequest(Method.POST,
                "myurl", successListener,
                errorListener){
            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
               Map<String, String> params = new HashMap<String, String>();
               params.put("regId", "skdjasjdaljdlksajskl");
               return params;
            }
        };

为什么凌空无视我的参数?

Why is Volley ignoring my parameters?

推荐答案

编辑:

我删除了我的previous的答案,因为它是不准确的。

I deleted my previous answer since it wasn't accurate.

我去了我今天所知道的: 显然, getParams能够应该工作。但它并不总是如此。 ?我已经调试它自己,它似乎正在执行PUT或POST请求时被调用,并在该方法中提供的PARAMS是一个普通的GET参数字符串(参数1 =值1&放大器;参数2 =值2 ...)和连接codeD,放在身上。

I'll go over what I know today: Apparently, getParams should work. But it doesn't always. I have debugged it myself, and it seems that it is being called when performing a PUT or POST request, and the params provided in that method are in a regular GET parameters string (?param1=value1&param2=value2...) and encoded and put in the body.

我不知道为什么,但出于某种原因,这并不为某些服务器正常工作。

I don't know why but for some reason this doesn't work for some servers.

最好的替代办法,我知道送的参数,就是把你的参数在的JSONObject 和连接code的请求的正文内容,使用请求构造函数。

The best alternate way I know to send parameters, is to put your parameters in a JSONObject and encode its contents in the request's body, using the request constructor.

阅读全文

相关推荐

最新文章