dougan6402 2019-04-14 06:45
浏览 83
已采纳

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

I am trying to populate the Postgres database through an android application. I am using Volley and Php to sent the data to the Postgres database. The Post request works well through browser i.e. database gets populated, which is checked through Postman extension of chrome. But when I do it through android I am unable to do so. I do not get any error as well.

Please find the below code where I send the x and y post parameters to the 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 {
            }
        });

Below is the PHP code

    <?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); 

?>

String URL = "http://192.168.1.107/test.php";

  • 写回答

1条回答 默认 最新

  • douliang1900 2019-04-14 06:57
    关注

    You are running your project in a virtual host, which is accessible to your browser, postman etc application. But it is not accessible to your phone to make a valid request.

    What you can do it is:

    1. Deploy your server project on a real server, which is a lot of work, but will worth it. Then you can access your api via a real ip address

    2. User emulator instead your phone

    3. Make your localhost accessible for you lan, so that you can access it via you phone. take a look at this answer for details

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题