安卓中的java改型在json中使用对象而不是数组
我使用改型来获取和解析Json,但我有问题
告诉我这个错误
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
我的json
{
"status": "ok",
"item": [
{
"name": "joe"
},
{
"name": "jack"
},
{
"name": "sara"
}
]
}
我的界面
public interface api{
@GET("/api/get.php")
Call<List<Repo>> listRepos();
}
主要活动
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(JacksonConverterFactory.create())
.build();
api service = retrofit.create(api.class);
Call<List<Repo>> call = service.listRepos();
call.enqueue(this);
及
@Override
public void onResponse(Response<List<Repo>> response, Retrofit retrofit) {
// specify an adapter (see also next example)
mAdapter = new RepoAdapter(response.body());
mRecyclerView.setAdapter(mAdapter);
for (Repo repo : response.body()) {
Log.i(TAG, repo.getName());
}
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
Log.e(TAG,t.getMessage());
}
我的模特
public class Repo {
private String name;
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我无法解析 谁能帮帮我吗。谢谢
# 1 楼答案
你的改型界面需要一个列表,但你的API并没有返回这个列表。您需要一个能够正确表示响应主体的类,例如:
然后,您的改造方法需要返回该类,而不是
List<Repo>