How to grab JSON Array and use gson to parse each json object? (Retrofit)

后端 未结 4 1447
忘掉有多难
忘掉有多难 2020-12-30 23:05

I am returning an array of results with my json Objects, and I am trying to use my customObjectResponse class to pull out each of the fields within each of the objects... th

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 23:54

    This can also work by just passing an array of response objects. So if this is your response object:

    public class CustomUserResponse {
        public String firstName;
        public String lastName;
        ...
    }
    

    You can use related syntax, depending on how you use the callbacks. Such as:

    new Callback(){
        @Override
        public void success(CustomUserResponse[] customUserResponses, Response rawResponse) {
    
        }
    
        @Override
        public void failure(RetrofitError error) {
    
        }
    };
    

    OR

    public class GetUserCommand implements Callback { ...
    

    Put simply, in every place where you normally replace T with a response class, replace it with an array, instead as in CustomUserResponse[].


    NOTE: to avoid confusing errors, be sure to also use an array in the Retrofit interface definition:

    @POST ( "/users" )
    public void listUsers(@Body GetUsersRequest request, Callback callback);
    

提交回复
热议问题