将参数传递到Android配置并将其发送到服务器

时间:2018-11-03 10:46:44

标签: php android

我想以编程方式将参数传递给php配置文件。

DbOperations.php中的代码:

function getName() {
    $stmt = $this->con->prepare("SELECT name,teamaffiliation 
    as team  FROM heroes WHERE id = 6");

    $stmt->execute();
    $stmt->bind_result($name, $team);

    $heroes = array(); 

    while($stmt->fetch()) {
        $hero  = array();
        $hero['name'] = $name; 
        $hero['team'] = $team;
        array_push($heroes, $hero); 
    }

    return $heroes; 
}

这是我的api.php:

case 'getname':
    $db = new DbOperation();
    $response['error'] = false; 
    $response['message'] = 'Request successfully completed';
    $response['heroes'] = $db->getname();
    break;

我想以编程方式传递ID(id = x)的值,请帮助我实现这一目标。

此外,我需要将参数从android传递到php。我尝试了以下代码,但这也不起作用。

private void retrive() {
    HashMap<String, String> params = new HashMap<>();
    params.put("id", "5");
    PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_READ_NAME, params, CODE_POST_REQUEST);
    request.execute();
}

1 个答案:

答案 0 :(得分:0)

对于RESTful API,您可以通过GET传递ID,因此您的URL看起来像/api.php?id=1

然后在api.php中,您可以使用$_GET['id'];访问该变量,并将其传递到getname函数中。

例如。

case 'getname':
    $db = new DbOperation();
    $response['error'] = false; 
    $response['message'] = 'Request successfully completed';
   $response['heroes'] = $db->getname($_GET['id']);
break;

然后,您可以在函数中将id = 6更改为id = ?,并使用?将参数($id)绑定到变量$stmt->bind_param("i", $id);上{{1 }}表示参数是整数,其他选项是:

  • i-整数
  • d-双(浮点)
  • s-字符串
  • b-BLOB

例如:

i