0

I have a JSON object that looks like this:

[[{
    "customers_id": 0
    "name": "John Doe",
    "address": "1234 Merry Way",
    "city": "Miami",
    "zipcode": "55443",
    "state": "Florida"
}, {
    "customers_id": 1
    "name": "John Doe",
    "address": "1234 Merry Way",
    "city": "Miami",
    "state": "Florida"
}, {
    "customers_id": 2
    "name": "John Doe",
    "address": "1234 Merry Way",
    "city": "Miami",
    "state": "Florida"
}],[]

When Retrofit returns, I get the error Expected BEGIN_OBJECT but was BEGIN_ARRAY. My call is set to return a list however:

@POST("search_clients.php")
Call<List<Course>> GetClients();

My actual retrofit call looks like this:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .build();

ClientLookup clientLookup = retrofit.create(ClientLookup.class);

Call<List<Client>> clients = clientLookup.GetClients();
clients.enqueue(new Callback<List<Client>>() {
    @Override
    public void onResponse(Response<List<Client>> response) {
        if (!response.isSuccess()) {
            Log.e(TAG, "No Success: " + response.message());
             return;
        }

        Log.d(TAG, response.body().toString());
    }

    @Override
    public void onFailure(Throwable t) {
        Log.e(TAG, "Failure: " + t.getMessage());
    }
});

And lastly my model looks like this:

public class Client {
  private int customers_id;
  private String name;
  private String address;
  private String city;
  private String zipcode;
  private String state;
}

And lastly a custom converter to remove the leading [ and trailing ,[]

public final class ClientCleaner extends Converter.Factory {
    private static final String TAG = ClientCleaner.class.getName();

    @Override
    public Converter<ResponseBody, Client> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new Converter<ResponseBody, Client>() {
            @Override
            public Client convert(ResponseBody body) throws IOException {
                String returnValue = body.string();
                if (returnValue.startsWith("[")){
                    returnValue = returnValue.substring(1);
                }
                if (returnValue.endsWith(",[]]")){
                    returnValue = returnValue.substring(0, returnValue.length() - 4);
                }
                Log.d(TAG, returnValue);
                Gson gson = new Gson();
                return gson.fromJson(returnValue, Client.class);
            }
        };
    }


}

Any idea why my call is still expecting a BEGIN_OBJECT?

ImDevinC
  • 508
  • 1
  • 4
  • 18
  • how does the raw response look like? – headsvk Mar 13 '16 at 21:14
  • Sorry, that's what I meant by JSON object. The first code block with the JSON format is the response. Although it initially comes in wrapped in another array. IE: [[{ "customers_id": 0 "name": "John Doe", "address": "1234 Merry Way", "city": "Miami", "zipcode": "55443", "state": "Florida" }, { "customers_id": 1 "name": "John Doe", "address": "1234 Merry Way", "city": "Miami", "state": "Florida" }],[] and I strip the first [ and last ,[] using a custom ConverterFactory – ImDevinC Mar 13 '16 at 21:16
  • no I mean did you check response.raw() if it really looks like what you want it to? – headsvk Mar 13 '16 at 21:17
  • I updated my reply with more details. I also can't check response.raw, because it's throwing into onFailure – ImDevinC Mar 13 '16 at 21:18
  • So you have a custom ConverterFactory, why isn't that in the question? – headsvk Mar 13 '16 at 21:20
  • Because I completely blanked, it's added to the original now ,as well as the original JSON response rather than the edited one – ImDevinC Mar 13 '16 at 21:29

1 Answers1

3

Deserializing of the list is not correct. Refer to this SO question about how to deserialize a list of objects with Gson.

This should be sufficient:

List<Client> videos = gson.fromJson(returnValue, new TypeToken<List<Client>>(){}.getType());
Community
  • 1
  • 1
headsvk
  • 2,726
  • 1
  • 19
  • 23
  • That was it thanks! After posting the ConverterFactory I realized it was probably the return value of that causing my issues, I just couldn't figure out how to return it as a list instead. Thanks for the insight! – ImDevinC Mar 13 '16 at 21:48