AndroLife
AndroLife

Reputation: 948

Retrofit Gson : Expected BEGIN_ARRAY but was BEGIN_OBJECT (Not duplicate)

Please I tried two approaches to solve this problem but both doesn't work, I don't know what did happen but before it works without problem. I have the following json :

{
  "_embedded" : {
    "posts" : [ {
      "postid" : 1,
      "taggedusers" : null,
      "hashtags" : null,
      "createdAt" : "2016-05-07",
      "commentsCount" : 0,
      "likesCount" : 0,
      "shareCount" : 0,
      "description" : "nothing",
      "post" : "nothing",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "postsEntity" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "usersByUserid" : {
          "href" : "http://localhost:8080/posts/1/usersByUserid"
        },
        "commentsesByPostid" : {
          "href" : "http://localhost:8080/posts/1/commentsesByPostid"
        }
      }
    }, {
      "postid" : 2,
      "taggedusers" : null,
      "hashtags" : null,
      "createdAt" : "2016-05-07",
      "commentsCount" : 0,
      "likesCount" : 0,
      "shareCount" : 0,
      "description" : "nothing",
      "post" : "nothing",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "postsEntity" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "usersByUserid" : {
          "href" : "http://localhost:8080/posts/2/usersByUserid"
        },
        "commentsesByPostid" : {
          "href" : "http://localhost:8080/posts/2/commentsesByPostid"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users/6/postsesByUserid"
    }
  }
}

I tried to read it using the following in retrofit :

 Retrofit retrofit = RetrofitGenerator.initRetrofit(CacheUtils.readSharedPreference(getActivity()).getToken());
        PostService postService= retrofit.create(PostService.class);
         postService.getUsersPost((int) CacheUtils.readSharedPreference(getActivity()).getUserID()).enqueue(new Callback<List<Post>>() {
             @Override
             public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                 if(response.isSuccessful()){
                     List<Post> postEntitiyList = response.body();

                 }
             }

             @Override
             public void onFailure(Call<List<Post>> call, Throwable t) {
                 Log.e("teeest",t.getMessage());
             }
         });

and for Interface :

public interface PostService {
    @Headers("Content-Type:application/json")
    @GET("/posts")
    Call<List<Post>> getAllPosts();

    @Headers("Content-Type:application/json")
    @GET("users/{id}/postsesByUserid")
    Call<List<Post>> getUsersPost(@Path("id") int id);
}

But I get this problem : Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ I tried to make the Post Pojo in another class like the following :

public class PostEntitiy {
    private Post post;

    public PostEntitiy(Post post) {
        this.post = post;
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }
}

Then I used List still I get the same problem , what the wrong thing I use?

Upvotes: 0

Views: 165

Answers (1)

totoro
totoro

Reputation: 2456

Expected BEGIN_ARRAY but was BEGIN_OBJECT

Your JSON is not a list it's an object: { ... } => object, [ ... ] => list.

EDIT

To get you started:

class Embedded {
    ...
}

class Links {
    ...
}

class Top {
    Embedded _embedded;
    Links _links;
}

and so on and so forth.

All of your List<Post> should then be replaces with Top.

Change the naming of my classes to fit your style.

Upvotes: 2

Related Questions