将post参数发送到服务器始终为Null

时间:2018-06-08 07:06:54

标签: android android-volley

我是Android开发新手,我遇到问题,我通过Volley将Post参数发送到服务器,但在服务器上我总是收到Null值。我无法在下面给出的代码中找到我的错误,请帮忙。

代码如下:

            String postJsonUrl = "https://example.com/activate_dealer.php";
            // Post params to be sent to the server
            HashMap<String, String> params = new HashMap<String, String>();
           // params.put("token", DeviceToken1);
            params.put("full_name", fname);
            params.put("mobile", mobile);
            params.put("password", pass);
            params.put("email", email);
            params.put("city", city);
            params.put("pincode", pincode);
            params.put("address", address);
            params.put("sponsor", sponsor);
            params.put("parent", parent);

            //JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            //        url, params, //Not null.
             //       new Response.Listener<JSONObject>()


            JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,postJsonUrl, new JSONObject(params),
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {



                                VolleyLog.v("Response:%n %s", response.toString(4));


                                String loginStatus = response.getString("Lstatus");
                                //String pi = response.getString("pi");

                                //Log.d("nn",nickname);
                                //Toaster.toast(loginStatus);

2 个答案:

答案 0 :(得分:1)

尝试这种方式发送请求

 StringRequest stringRequest = new StringRequest(Request.Method.POST, postJsonUrl,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                            }
                        }){
                    @Override
                    protected Map<String,String> getParams(){
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("full_name", fname);
            params.put("mobile", mobile);
            params.put("password", pass);
            params.put("email", email);
            params.put("city", city);
            params.put("pincode", pincode);
            params.put("address", address);
            params.put("sponsor", sponsor);
            params.put("parent", parent);
                        return params;
                    }

                };

                getRequestOtpPage().addToRequestQueue(stringRequest);return params;
                    }

                };

            getRequestOtpPage().addToRequestQueue(stringRequest);

答案 1 :(得分:0)

我必须检索JSON对象而不是$ _POST,这解决了我的问题

$ _ POST = json_decode(file_get_contents('php:// input'),true);