jeudi 16 juin 2016

Retrofit2 + Gson IllegalStateException while fetching JSONArray

I've just recently started using Retrofit + Gson and though I've looked around for an answer to my problem and tried several solutions, nothing seems to be working, instead resulting with:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Firstly, the JSONArray that gets returned looks something like this:

[{
    "id": 23545,
    "title": "some title",
    "description": "some description",
    "questions": {
        "text": [{
            "question": "Some question"
        }, {
            "question": "Some other question"
        }],
        "checkbox": [{
            "question": "Some question",
            "options": [{
                "value": "3",
                "correct": "1"
            }, {
                "value": "2",
                "correct": "0"
            }, {
                "value": "1",
                "correct": "0"
            }]
        }]
     }
}]

This is my Retrofit client...

public class ApiClient {

    public static final String BASE_URL = "http://somewhere.com/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        }
        return retrofit;
    }

    public static ApiInterface getApiInterface() {
        return getClient().create(ApiInterface.class);
    }

}

This is the interface...

public interface ApiInterface {

    @Headers("Content-Type: application/x-www-form-urlencoded")
    @GET("rest/reviews.json")
    Call<Reviews> getReviews(@Header("Cookie") String cookie);

}

This is the Reviews model:

public class Reviews {

    private ArrayList<Review> reviews = new ArrayList<>();

    public ArrayList<Review> getReviews() {
        return reviews;
    }

    public void setReviews(ArrayList<Review> reviews) {
       this.reviews = reviews;
    }

}

And the Review model...

public class Review {

    @SerializedName("id") private Integer id;
    @SerializedName("title") private String title;
    @SerializedName("description") private String description;
    @SerializedName("questions") private Questions questions;

    public String getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    etc...
}

And the fragment code where I make the request...

ArrayList<Review> reviews = new ArrayList<>();

ApiInterface apiInterface = ApiClient.getApiInterface();

Call<Reviews> call = apiInterface.getAppraisals(cbi.onGetCookie());

call.enqueue(new Callback<Reviews>() {
    @Override
    public void onResponse(Call<Reviews> call, Response<Reviews> response) {
        if(response.isSuccessful()) {
            reviews = response.body().getReviews();
            // pass on to RecyclerView adapter
        } else {
            // handle fail
        }
    }
    @Override
    public void onFailure(Call<Reviews> call, Throwable t) {
        Log.e(TAG, t.toString());
    }
});

Gradle dependencies in case anyone's interested...

compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

I understand the error message, I'm just trying to figure out how to make Gson expect a JSONArray. I'm sure my mistake is obvious but I'm not seeing or understanding it. Thanks in advance for any help!

Aucun commentaire:

Enregistrer un commentaire