如何让 PHP 使用 Retrofit?

我想使用这样的改造界面。


public interface ApiInterface {


    @GET("test.php")

    Call<Person> getPerson(@Query("name") String keyword);

}

我有这个错误,因为我已经像这样挤压并执行了 php 。


com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

我想用Call<Person>,我不想用Call<List<Person>>


这是我制作的 php。


<?php


require_once 'conn.php';


if(isset($_GET['name'])) {

    $name = $_GET['name'];

    $query = "SELECT `NAME`, AGE, `ADDRESS` FROM test WHERE `NAME` = '$name'";

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


    $response = array();

    while($row = mysqli_fetch_assoc($result)) {

        array_push(

            $response, array(

                'name'=>$row['NAME'],

                'age'=>$row['AGE'],

                'address'=>$row['ADDRESS'])

            );

    }


    echo json_encode($response);

}


mysqli_close($conn);


?>

这是 POJO 人。


public class Person {

    @SerializedName("name") private String name;

    @SerializedName("age") private int age;

    @SerializedName("address") private String address;


    public String getName() {

        return name;

    }


    public void setName(String name) {

        this.name = name;

    }


    public int getAge() {

        return age;

    }


    public void setAge(int age) {

        this.age = age;

    }


    public String getAddress() {

        return address;

    }


    public void setAddress(String address) {

        this.address = address;

    }

}


凤凰求蛊
浏览 97回答 2
2回答

人到中年有点甜

正如评论中所解释的,您的问题是由于 API 调用返回一个 Person 数组而引起的,而您希望收到一个 Person 对象。这就是为什么错误指出Expected BEGIN_OBJECT but was BEGIN_ARRAY您可以:A) 更改您的 php 代码以返回一个对象而不是数组。或者B) 按如下方式更改 Java 代码:API接口public interface ApiInterface {&nbsp; &nbsp; @GET("test.php")&nbsp; &nbsp; Call<List<Person>> getPerson(@Query("name") String keyword);}主要活动&nbsp; &nbsp; public void personList(String key) {&nbsp; &nbsp; &nbsp; &nbsp; apiInterface = ApiClient.getApiClient().create(ApiInterface.class);&nbsp; &nbsp; &nbsp; &nbsp; Call<List<Person>> call = apiInterface.getPerson(key);&nbsp; &nbsp; &nbsp; &nbsp; call.enqueue(new Callback<List<Person>>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check that the list of objects is not empty before trying to read first entry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!response.body.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Take the first entry in the List&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Person person = response.body().get(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nameText.setText(person.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Name : " + person.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Call<List<Person>> call, Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("onFailure", t.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }最后,我也认为@Query应该改为@Field

慕妹3242003

我不懂PHP,但是jsonsyntaxException是由JSON解析错误引起的。你可以尝试在Android中打印jsonstr然后进行转换。我希望它能帮助你@GET("test.php")调用 getPerson(@Query("name") String 关键字);
打开App,查看更多内容
随时随地看视频慕课网APP