【发布时间】:2018-06-11 00:12:31
【问题描述】:
我正在尝试从 http://jsonplaceholder.typicode.com/users 获取数据并将其显示在 recyclerview 中。我不需要 api 中的所有列
公共接口 APIService {
@GET("/users")
Call<JSONResponse>getJSON();}
公共类 JSONResponse {
private UserInfo[] Info;
public UserInfo[] getInfo(){
return Info;
}}
。 公共类用户信息 {
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("city")
private String city;
@SerializedName("company")
private String company;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("lat")
private double lat;
@SerializedName("lng")
private double lng;
@SerializedName("website")
private String website;
//getter methods }
在主要活动中
private void loadJSON() {
Call<JSONResponse>call = apiService.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getInfo()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse>call,Throwable t) {
Log.d("Error", t.getMessage());
}
});
}
我收到 java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $.
我尝试更改为Call<List<UserInfo>> getJSON();,但我得到了 java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 19 column 17 path $[0].company
【问题讨论】:
标签: java android json retrofit