无法检测 android 发送的 php 中的 Post 参数

unable to detect Post parameters in php sent from android

我正在尝试通过 android 应用程序填充 Postgres 数据库。我正在使用 Volley 和 Php 将数据发送到 Postgres 数据库。 Post 请求通过浏览器运行良好,即填充数据库,这是通过 chrome 的 Postman 扩展检查的。但是当我通过 android 这样做时,我无法这样做。我也没有收到任何错误。

请找到下面的代码,我将 x 和 y post 参数发送到 URL。

submitbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                builder.setTitle("Server Response");
                builder.setMessage("Response: "+response);
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Soil_Info.this, "Clear fields here", Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog alertDialog =builder.create();
                alertDialog.show();
                Toast.makeText(Soil_Info.this, "Successfull", Toast.LENGTH_SHORT).show();

            }
        }

                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(Soil_Info.this, "Error Occured", Toast.LENGTH_SHORT).show();
                error.printStackTrace();

            }
        }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map <String,String> params=new HashMap<>();

                params.put("x", "aisha");
                params.put("y", "shah");
                return params;
            }
        };


        MySingleton.getInstance(Soil_Info.this).addTorequestque(stringRequest);

        stringRequest.setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 50000;
            }

            @Override
            public int getCurrentRetryCount() {
                return 50000;
            }

            @Override
            public void retry(VolleyError error) throws VolleyError {
            }
        });

下面是PHP代码

    <?php
require "connection.php";


if(isset($_REQUEST["x"]) && isset($_REQUEST["y"]))
{
    $names = $_REQUEST["x"];
    $surnames = $_REQUEST["y"];


    // echo $names ;
    // echo $surnames ;

    $query = "INSERT INTO emp (first, last) VALUES ('$names','$surnames')";
    echo $query;

    $result = pg_query($conn, $query);

    if (!$result) {
        die('An error occurred during query.');
    } else {
      echo ".....Successfull.....";
    }   
   }
  else
   {
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
  }



pg_close($conn); 

?>

字符串 URL = "http://192.168.1.107/test.php";

您是 运行 您在虚拟主机中的项目,您的浏览器、邮递员等应用程序都可以访问它。但是您的 phone 无法访问它来发出有效请求。

你能做的是:

  1. 将您的服务器项目部署到真实服务器上,这需要大量工作,但值得。然后你就可以通过一个真实的IP地址访问你的api

  2. 用户模拟器而不是你的 phone

  3. 让您的 localhost 可以被您的 lan 访问,这样您就可以通过您 phone 访问它。查看 this 答案了解详情