[英]How to Get Json object and Json Array in Same KEY by retrofit for android
[英]Retrofit in android - use object Instead of array in json
我将Retrofit用于get和Parse Json,但我有问题
告诉我这个错误
java.lang.IllegalStateException:应为BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT
我的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;
}
}
我无法解析有人可以帮助我。 谢谢
您的改装接口需要一个列表,但这不是您的API返回的列表。 您需要一个可以正确表示响应正文的类,例如:
class ListReposResponse {
String status;
List<Repo> item;
}
然后,您的改造方法需要返回该类,而不是List<Repo>
。
@GET("/api/get.php")
Call<ListReposResponse> listRepos();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.