排球字符串请求 post 参数在 php 中始终为空。

Volley string request post parameters always null in php.

我正在发送一个带有一些参数的字符串 POST 请求,但在 PHP 脚本中我总是将参数值设置为 null。当我用邮递员测试 PHP 时,它工作正常。

这是我的截击请求代码:

public void login(final String mPhone, final String mEmail, final String mPassword) {


        StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.LOGIN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getBoolean("error")) {
                        String error = jsonObject.getString("message");
                        if (error.equals("new user")) {
                            registerUser();
                        } else {
                            showProgress(false);
                            Snackbar.make(mOtpView, error, Snackbar.LENGTH_SHORT).show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                showProgress(false);
                Snackbar.make(mOtpView, error.getMessage(), Snackbar.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("phone", mPhone);
                params.put("email", mEmail);
                params.put("password", mPassword);

                return params;
            }
        };
        //Method to limit retry policy of request
        stringRequest.setRetryPolicy(
                new DefaultRetryPolicy(
                        DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
                        3,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                )
        );
        try {

            //Adding request to request queue
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的 PHP:

require_once 'DbConfig.php';

$response = array();

if (isset($_GET['apicall'])) {
    switch ($_GET['apicall']) {
        case 'login':

            $phone = $_POST['phone'];
            $email = $_POST['email'];
            $pass = $_POST['password'];

            echo $phone.$email.$pass;exit();

我已经尝试通过覆盖 getHeaders() 添加 header 并且还尝试覆盖 getBodyContent() 但似乎没有任何效果而且当我在本地主机中使用它时上面的代码工作正常。我检查了 URL 它很好,因为 PHP 脚本很好,因为我已经通过回显它执行的其他事情来测试它。

通常这个问题可能来自 Content-Type 添加 charset=utf-8 :

 params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

来源:

您只是发送 ;

params.put("phone", mPhone); params.put("email", mEmail); params.put("password", mPassword);

怎么样; params.put("apicall","YOu_API");

我猜 apicall 没有被设置,所以值将为空。

已解决!!!不能考虑这个!实际上我在使用 URL 就像:- http://www.example.com but when i tried http://example.com .. 它很有魅力......不过还是感谢大家的帮助!!