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

TA貢獻1824條經驗 獲得超6個贊
我不懂PHP,但是jsonsyntaxException是由JSON解析錯誤引起的。你可以嘗試在Android中打印jsonstr然后進行轉換。我希望它能幫助你
@GET("test.php")
調用 getPerson(@Query("name") String 關鍵字);
- 2 回答
- 0 關注
- 165 瀏覽
添加回答
舉報