无法使用 Volley 库将数据传递给 POST 请求 - Android

Unable to pass data to a POST request using Volley library - Android

我正在尝试从我的服务器发送一个 firebase 推送通知。我通过像这样使用 Volley 发送 POST 请求来触发 .php 文件:

public static void dataPacket(Activity activity, Map<String, String> bundle)
    {
        new AsyncTask<String, Boolean, Boolean>()
        {
            protected Boolean doInBackground(String... params)
            {
                String url  = CloudData.TEST_DATA_PACKET;
                RequestQueue queue = Volley.newRequestQueue(activity);

                StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                        response -> {
                            // response
                            Log.d("Response", response);
                        },
                        error -> {
                            error.printStackTrace();
                            Log.d("Error.Response", error.toString());
                        }
                ) {
                    @Override
                    protected Map<String, String> getParams()
                    {
                        return bundle;
                    }

                    @Override
                    public Map<String, String> getHeaders()
                    {
                        Map<String, String>  headerParams = new HashMap<>();
                        headerParams.put("Content-Type", "application/json; charset=utf-8");

                        return headerParams;
                    }//*/
                };

                postRequest.setRetryPolicy(new DefaultRetryPolicy(
                        10000,
                        0,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));//*/

                queue.add(postRequest);
                return true;
            }
        }.execute();
    }

这是我服务器中的 .php 文件:

<?php
    require 'FCMPushNotification.php'; 

    $FCMPushNotification = new \BD\FCMPushNotification('MY_API_KEY');

    $sDeviceToken = $_POST['token'];

    $aPayload = array(
        'data' => array("test"=>123),
        'notification' => array(
            'title' => 'Example app',
            'body'=> 'This is an example message',
            'sound'=> 'default'
        )
    );

    $aOptions = array(
        'time_to_live' => 0 //means messages that can't be delivered immediately are discarded. 
    );

    $aResult = $FCMPushNotification->sendToDevice(
        $sDeviceToken,      
        $aPayload,
        $aOptions // optional
    );

    var_dump($aResult);  

 ?>

当我改变这一行时

$sDeviceToken = $_POST['token'];

至此

$sDeviceToken = 'my_firebase_token_for_a_device';

一切正常。但是,当我尝试从 POST 请求 android 获取变量 'token' 时,出现此错误:

BasicNetwork.performRequest: Unexpected response code 500 for https://mywebsite.com/sendnotification.php W/System.err: com.android.volley.ServerError W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:179) W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114) D/Error.Response: com.android.volley.ServerError

我做错了什么?

更新

这就是我调用 dataPacket 的方式:

PostTaskListener<String> postTaskListener = result ->
        {
            bundle.put("token",result); 
            SendNotification.dataPacket(activity,bundle);
        };
        FirebaseCloud.getFirebaseToken(db.getActiveProfile().cID,postTaskListener,activity);

您将正文格式指定为 application/json,并作为数组(表单数据)输入,这可能会影响结果。格式应为 multipart/form-data(或 application/x-www-form-urlencoded,根据 wiki)。