从改造 @Body 表单中检索 php 中的数据的问题

Issue with retrieving data in php from retrofit @Body form

我在 android studio 和改造中用@Body 注释编写了一些用于发布数据的代码,但我无法检索到 php。它只是给我一个空白变量

这是我的android代码

@POST("process_post.php")
Call<Response> sendPost(@Body ExamplePost post);

这是 ExamplePost 变量

@SerializedName("name")
private String guitaristName ;
@SerializedName("guitar")
private String guitaristGuitar;
@SerializedName("age")
private int guitaristAge ;

最后这是我的 php 代码

<?php


if ($_SERVER['REQUEST_METHOD'] == "POST"){

    $json = file_get_contents('php//input');
    $data = json_decode($json,true);
    $name = $data['name'];
    $guitar = $data['guitar'];
    $age = $data ['age'];

    $response = array();
    $response["code"] = 1;
    $response["server"] = $name . " : " . $guitar . " : " . $age;
    echo json_encode($response);
}

您的代码中存在语法错误。试试这个。

$json = file_get_contents('php://input');