无法使用截击将参数发送到服务器

can't send params to server using volley

我不知道为什么我不能使用 volley 将参数发送到服务器,我的问题是当我尝试使用 velley 发送参数时,我的 logcat、

[149] BasicNetwork.performRequest: Unexpected response code 403 for http://localhost/test/post.php

大家可以帮我解决这个问题吗?

我的 php 服务器:

<?php
if (isset($_POST['action'])){
    $action = $_POST['action'];
} else {
    echo "Invalid Data";
exit;
}
if($action="read"){
    echo "read";
}
?>

我的要求:

    String  tag_string_req = "string_req";

    String url = "http://localhost/test/post.php";


    StringRequest strReq = new StringRequest(Request.Method.POST,
            url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d("TAG", response.toString());
            Log.d("LOG", "success");

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("TAG", "Error: " + error.getMessage());
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "read");
            return params;
        }
    };
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

我认为问题出在您的 URL 参数上。 "localhost"是与设备本身连接的词,你的服务器不能运行。 您需要写 运行 连接服务器的计算机的 IP,而不是本地主机。

示例:“http://replace_it_with_the_ip/test/post.php”;