亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓 PHP 使用 Retrofit?

如何讓 PHP 使用 Retrofit?

PHP
鳳凰求蠱 2024-01-19 15:19:01
我想使用這樣的改造界面。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。<?phprequire_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;    }}
查看完整描述

2 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

正如評論中所解釋的,您的問題是由于 API 調用返回一個 Person 數組而引起的,而您希望收到一個 Person 對象。這就是為什么錯誤指出Expected BEGIN_OBJECT but was BEGIN_ARRAY


您可以:


A) 更改您的 php 代碼以返回一個對象而不是數組。


或者


B) 按如下方式更改 Java 代碼:


API接口


public interface ApiInterface {


    @GET("test.php")

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

}

主要活動


    public void personList(String key) {

        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

        Call<List<Person>> call = apiInterface.getPerson(key);

        call.enqueue(new Callback<List<Person>>() {

            @Override

            public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {

                // Check that the list of objects is not empty before trying to read first entry

                if (!response.body.isEmpty()) {

                    // Take the first entry in the List

                    Person person = response.body().get(0);

                    nameText.setText(person.getName());

                    System.out.println("Name : " + person.getName());

                }

               

            }


            @Override

            public void onFailure(Call<List<Person>> call, Throwable t) {

                Log.e("onFailure", t.toString());

            }

        });

    }

最后,我也認為@Query應該改為@Field


查看完整回答
反對 回復 2024-01-19
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

我不懂PHP,但是jsonsyntaxException是由JSON解析錯誤引起的。你可以嘗試在Android中打印jsonstr然后進行轉換。我希望它能幫助你

@GET("test.php")
調用 getPerson(@Query("name") String 關鍵字);


查看完整回答
反對 回復 2024-01-19
  • 2 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號