当我尝试使用Retrofit从我的api获取数据时收到此错误消息:
W / MainActivity:onFailure客户端:预计BEGIN_ARRAY但是 路径$
的BEGIN_OBJECT
我通读了几个主题,主要是说我制作了一个,但它仍然不起作用。
这是获取数据的方法
public void getClient() {
callClient = service.client();
callClient.enqueue(new Callback<List<ClientResponse>>() {
@Override
public void onResponse(Call<List<ClientResponse>> call, Response<List<ClientResponse>> response) {
Log.w(TAG, "onResponse getClient: " + response );
if(response.isSuccessful()) {
Log.w(TAG, "getClient getData successful: " + response.body().get(0));
} else {
Log.w(TAG, "getClient not successful");
}
}
@Override
public void onFailure(Call<List<ClientResponse>> call, Throwable t) {
Log.w(TAG, "onFailure client: " + t.getMessage() );
}
});
}
我的ApiService接口
public interface ApiService {
...
@GET("client")
Call<List<ClientResponse>> client();
...
}
My Client Model
public class Client {
public int id;
public String name;
public String email;
public String created_at;
public String updated_at;
public Client(int id, String name, String email, String created_at, String updated_at) {
this.id = id;
this.name = name;
this.email = email;
this.created_at = created_at;
this.updated_at = updated_at;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
我的ClientResponse
public class ClientResponse {
List<Client> data;
public List<Client> getData() { return data; }
public void setData(List<Client> data) { this.data = data; }
}
这是我当前的json输出的样子,我是从postman复制的:
{
"data": {
"id": 1,
"name": "Mad Max",
"email": "mad@max.com",
"created_at": "2017-07-26 09:57:22",
"updated_at": "2017-07-26 09:57:22"
}
}
答案 0 :(得分:1)
这意味着即将到来的回复为object
,但您当前的回复是Array
您的回复List<ClientResponse>
我觉得它应该是ClientResponse
对象
public class ClientResponse {
public Client data;
public Client getData() { return data; }
public void setData(Client data) { this.data = data; }
}
获取数据
public void getClient() {
callClient = service.client();
callClient.enqueue(new Callback<List<ClientResponse>>() {
@Override
public void onResponse(Call<List<ClientResponse>> call, Response<List<ClientResponse>> response) {
if(response.isSuccessful()) {
// first way to get data
Client client =response.body().data;
// second way but i dont prefer it .but you want it as per ou r
//discussion
response.body().setData(response.body().data);
Client client =response.body().getData();
// third way to get data
ClientResponse clientResponse = response.body();
Log.w(TAG, "getClient getData successful: " +
clientResponse.getData().getName());
} else {
Log.w(TAG, "getClient not successful");
}
}
@Override
public void onFailure(Call<List<ClientResponse>> call, Throwable t) {
Log.w(TAG, "onFailure client: " + t.getMessage() );
}
});
}
答案 1 :(得分:0)
我认为您可能应该将ApiService接口修改为
@GET("client")
Call<ClientResponse> client();
和ClientResponse
应为
public class ClientResponse {
public Client data;
public Client getData() { return data; }
public void setData(Client data) { this.data = data; }
}