php - Android PHP Mysql 通过params获取数据

标签 php android mysql android-volley

我正在制作与 PHP MySQL 通信的 Android 应用程序 有PHP文件

create_order.php
get_all_orders.php
get_order_status.php

create_order.php 有效 get_all_orders.php 也 但在浏览器中的示例中,我输入 get_order_status.php?oid=102 它给了我响应,但包含错误 {"success":0,"message":"必需的字段缺少获取状态"} 通过 POST 方法从 android 尝试时

RequestQueue queue = MyVolley.getRequestQueue();

        String num1 = "1";

        if (num1 != null && !num1.equals("")) {
            String uri = String
                    .format("http://192.168.0.101/android_connect/get_order_status.php?param1=%1$s",
                            num1);

            StringRequest myReq = new StringRequest(Request.Method.GET,
                    uri,
                    createMyReqSuccessListener(),
                    createMyReqErrorListener());
            queue.add(myReq);
        }

获取订单状态.php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// include db connect class
//require_once __DIR__ . '/db_connect.php';

// connecting to db
//$db = new DB_CONNECT();

// check for post data

//echo("post data - ".$_GET['oid']);

if (isset($_GET['oid'])) {
    $pid = $_GET['oid'];

    // get a order from orders table
 $mysqli = new mysqli('localhost','root','rootpass','testdb');

 $result = mysqli_fetch_array($mysqli->query("SELECT * FROM orders WHERE oid = $pid")); 
     //echo($result["status"]);
    if (count($result)>0) {
        // check for empty result


            $order = array();
            $order ["status"] = $result["status"];

            // success
            $response["success"] = 1;

            // user node
            $response["order"] = array();

            array_push($response["order"], $order);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No order found";

            // echo no users JSON
            echo json_encode($response);
        }

} else {
    // required field is missing
    $response["success"] = 0;
   $response["message"] = "Required field(s) is missing get status";

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

最佳答案

将这一行中的param1更改为oid(android代码)

String uri = String.format("http://192.168.0.101/android_connect/get_order_status.php?param1=%1$s",num1);

应该是这样的:

String uri = String.format("http://192.168.0.101/android_connect/get_order_status.php?oid=%1$s",num1);

在您的 PHP 文件中,您正在读取名为 oid 的参数

$pid = $_GET['oid'];

关于php - Android PHP Mysql 通过params获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36675291/

相关文章:

php - Javascript 或 jQuery 中的计数器

php - 强制 PHP 不包含/要求某些文件

php 将 Id 传递到下一页和从数据库

android - 如何获取 Google Play 应用的 beta 用户数量

android - 对话框背景填充的默认颜色是什么?

android - List View Item 行点击 Caused by : java. lang.NumberFormatException

PHP – 无法让左连接表进行回显

php - 只有使用 php 登录才能访问页面

php - 如何从数据库表中检索所有行并遍历每一行以匹配密码 PHP

mysql - 旋转 SQL 表