Android改造onFailure应为BEGIN_数组,但为BEGIN_对象

Android改造onFailure应为BEGIN_数组,但为BEGIN_对象,android,arrays,object,retrofit,Android,Arrays,Object,Retrofit,当我尝试使用改型从api获取数据时,我收到此错误消息: W/MainActivity:onFailure客户端:应为BEGIN\u阵列,但为 在路径处开始对象$ 我读了几篇文章,主要是说我做了一篇文章,但它仍然不起作用 这是获取数据的方法 public void getClient() { callClient = service.client(); callClient.enqueue(new Callback<List<ClientResponse>>

当我尝试使用改型从api获取数据时,我收到此错误消息:

W/MainActivity:onFailure客户端:应为BEGIN\u阵列,但为 在路径处开始对象$

我读了几篇文章,主要是说我做了一篇文章,但它仍然不起作用

这是获取数据的方法

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() );
        }
    });
}
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() );
        }
    });
}

这意味着即将到来的响应是对象,但您当前的响应是数组

您的响应列表,我认为它应该是ClientResponse对象

获取数据

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() );
        }
    });
}
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() );
        }
    });
}

我认为您可能应该将您的ApiService接口修改为

@GET("client")
Call<ClientResponse> client();

这意味着您得到的响应是一个对象,而不是arrycan u post您的json responsepost响应jsonformat@Anil我用json响应/formatHi-ok更新了这个问题,我没有得到错误,我得到了一个200,这很好,但是我也没有得到任何数据响应。isSuccessful返回我这个getClient getData successful:com.fooj.myapp.entities。Client@c5dadf5,如何显示/返回数据?只需使用response.body.data,您应该使用我在回答中提到的ClientResponse不起作用我正在使用此response.body.getData getData方法来自我的客户端响应公共客户端getData{return data;}好的,您应该在检索数据之前先传递数据。因此,请首先传递数据response.body.setDataresponse.body.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() );
        }
    });
}
@GET("client")
Call<ClientResponse> client();
  public class ClientResponse {
     public Client data;
     public Client getData() { return data; }
        public void setData(Client data) { this.data = data; }
  }