【问题标题】:Retrofit Gson : Expected BEGIN_ARRAY but was BEGIN_OBJECT (Not duplicate)改造 Gson:预期为 BEGIN_ARRAY,但为 BEGIN_OBJECT(不重复)
【发布时间】:2016-05-07 21:46:31
【问题描述】:

请我尝试了两种方法来解决这个问题,但都不起作用,我不知道发生了什么,但在它正常工作之前没有问题。 我有以下 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"
    }
  }
}

我尝试在改造中使用以下内容阅读它:

 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());
             }
         });

对于接口:

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);
}

但我遇到了这个问题:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处是 BEGIN_OBJECT 我试图在另一个类中制作 Post Pojo,如下所示:

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;
    }
}

然后我用List还是遇到同样的问题,我用什么错了?

【问题讨论】:

    标签: android gson retrofit retrofit2


    【解决方案1】:

    应为 BEGIN_ARRAY,但为 BEGIN_OBJECT

    您的 JSON 不是一个列表,它是一个对象:{ ... } => 对象,[ ... ] => 列表。

    编辑

    让您开始:

    class Embedded {
        ...
    }
    
    class Links {
        ...
    }
    
    class Top {
        Embedded _embedded;
        Links _links;
    }
    

    等等等等。

    您的所有List&lt;Post&gt; 都应替换为Top

    更改我的课程名称以适合您的风格。

    【讨论】:

    • 是的,您需要设计具有与 JSON 名称和类型匹配的属性的 Java 对象的层次结构。
    • @H-Ideas 我编辑了入门想法。我不精通改装,所以这是我能做的最好的:-)
    • 谢谢,似乎还有另一个“奇怪”的问题我尝试了你的解决方案我得到了同样的错误我测试了其他解决方案。
    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 2015-07-24
    • 2014-08-01
    • 2015-11-26
    • 2018-03-27
    • 2016-06-28
    • 2019-02-06
    • 1970-01-01
    相关资源
    最近更新 更多