Volley JsonObjectRequest Post request not working

后端 未结 9 1839
有刺的猬
有刺的猬 2020-11-22 06:25

I am using android Volley for making a request. So I use this code. I don\'t understand one thing. I check in my server that params is always null. I consider that getParams

相关标签:
9条回答
  • 2020-11-22 07:23

    You can create a custom JSONObjectReuqest and override the getParams method, or you can provide them in the constructor as a JSONObject to be put in the body of the request.

    Like this (I edited your code):

    JSONObject obj = new JSONObject();
    obj.put("id", "1");
    obj.put("name", "myname");
    
    RequestQueue queue = MyVolley.getRequestQueue();
    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,obj,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                 System.out.println(response);
                 hideProgressDialog();
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                 hideProgressDialog();
            }
        });
    queue.add(jsObjRequest);
    
    0 讨论(0)
  • 2020-11-22 07:25

    I had the same issue once, the empty POST array is caused due a redirection of the request (on your server side), fix the URL so it doesn't have to be redirected when it hits the server. For Example, if https is forced using the .htaccess file on your server side app, make sure your client request has the "https://" prefix. Usually when a redirect happens the POST array is lost. I Hope this helps!

    0 讨论(0)
  • 2020-11-22 07:26

    All you need to do is to override getParams method in Request class. I had the same problem and I searched through the answers but I could not find a proper one. The problem is unlike get request, post parameters being redirected by the servers may be dropped. For instance, read this. So, don't risk your requests to be redirected by webserver. If you are targeting http://example/myapp , then mention the exact address of your service, that is http://example.com/myapp/index.php.
    Volley is OK and works perfectly, the problem stems from somewhere else.

    0 讨论(0)
提交回复
热议问题