Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

android - Google Volley ignores POST-Parameter

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

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.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had same issue last week, but it is fixed now. Your server accepts the Content-Type as form-data, when sending volley's JsonObjectRequest the request's content-type will be application/json so whole params will be sent as one json body, not as key value pairs as in Stringrequest. Change the server code to get request params from http request body instead of getting it from keys(like $_REQUEST['name'] in php).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...