Java – Integration of GreenDao with Retrofit

Integration of GreenDao with Retrofit… here is a solution to the problem.

Integration of GreenDao with Retrofit

I was looking for a solution that would allow me to combine work retrofit and GreenDao.

Here is my code and it doesn’t work.

Post is a class generated by the greenDao generator

EDIT: The run of this code returns “retrofit. RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but be BEGIN_OBJECT at line 1 column 2 path $

public static void test() {
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(BASE_URL)
            .build();

PostsInterface postsInterface = restAdapter.create(PostsInterface.class);

Callback<List<Post>> callback = new Callback<List<Post>>() {
        @Override
        public void success(List<Post> posts, Response response) {
            Log.d(TAG, response.toString());
        }

@Override
        public void failure(RetrofitError error) {
            Log.d(TAG, error.toString());
        }
    };

postsInterface.getPosts(0, 10, callback);
}

public interface PostsInterface {
    @GET("/posts")
    void getPosts(@Query("start") int limit, @Query("count") int offset, Callback<List<Post>> callback);
}

Solution

It looks like the request returns a JSON object instead of a JSON array. Make sure that the server actually returns a list of Post, and that your Post object does not expect named fields as arrays when the Post from the server contains ordinary objects with the same name.

Related Problems and Solutions