how to send post request with data in Volley?

£可爱£侵袭症+ 提交于 2019-12-06 11:52:13

问题


This is my android code

           JSONObject params = new JSONObject();
           params.put("username","rajesh@gmail.com");
           params.put("password","hellothere");

           JsonObjectRequest loginRequest = new JsonObjectRequest(
                    Request.Method.POST,
                    "http://192.168.2.67/tmp/test.php",
                    params,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                        Log.d("","");

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                        Log.d("","");
                        }
                    });


            requestQueue.add(loginRequest);

The server code in php

            <?php
               $usr = $_REQUEST['username'];
                $pwd = $_REQUEST['password'];
                $resp[$usr]=$pwd;
                echo json_encode($resp);
               ?>

the response i'm getting is {"":null}

I tried with apache http cleint and it worked prferectly is there any way i can do this with volley?


回答1:


To send parameters in request body you need to override either getParams() or getBody() method of the request classes

Source: Asynchronous HTTP Requests in Android Using Volley




回答2:


@Override
protected Map<String, String> getParams() 
{  
Map<String, String>  params = new HashMap<String, String>();  
    params.put("username", "SomeUser");  
    params.put("password", "blabla");



 return params;  
}

This is the way you should override getParams() method.



来源:https://stackoverflow.com/questions/19429426/how-to-send-post-request-with-data-in-volley

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!