Android - Retrofit异常预期BEGIN_OBJECT但是BEGIN_ARRAY

时间:2015-02-19 13:34:45

标签: android gson retrofit

我是GSON的新手并进行改造,这是我的输出模型(仅供参考结构),

    public class Result1
    {
        public int TotalCount { get; set; }
    }

    public class Result2
    {
        public int PostId { get; set; }
        public int PostTypeId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool IsFeatured { get; set; }
        public string Thumbnail { get; set; }
        public string CreatedDate { get; set; }
        public int CreatedBy { get; set; }
    }



public class RootObject
  {
     @Expose
        private List<com.example.got_sample.Result2> Result2 = new ArrayList<com.example.got_sample.Result2>();

    public List<com.example.got_sample.Result1> getResult1() {
        return Result1;
        }

        public void setResult1(List<com.example.got_sample.Result1> Result1) {
        this.Result1 = Result1;
        }

        public List<com.example.got_sample.Result2> getResult2() {
        return Result2;
        }
        public void setResult2(List<com.example.got_sample.Result2> Result2) {
        this.Result2 = Result2;
        }
    //Similar for result1
            }

这是我的输出

    [{"Result1":

[{"TotalCount":5}]},

    {"Result2":

    [{"PostId":6,"PostTypeId":1,"Title":"","Description":"something"..},{"PostId":7,"PostTypeId":1,"Title":"","Description":"something"..}]
    }]

这是我的改装代码

 postmethod.sendpostrequest(object, new Callback<RootObject>() {
                      @Override
                      public void failure(RetrofitError retrofitError) {
                          System.out.println(retrofitError.getMessage());
                      }

                    @Override
                    public void success(RootObject arg0,
                            retrofit.client.Response arg1) {
                        // TODO Auto-generated method stub

                    }
                  });

    This is the exception I get"
  

预计BEGIN_OBJECT但是BEGIN_ARRAY“

我尝试将RootObject替换为List&lt;结果2&gt;因为我只对结果2感兴趣。在这种情况下,我得到的响应为null。请帮助。

1 个答案:

答案 0 :(得分:1)

JSON的根是Result1对象的数组,但您的Callback方法适用于单个RootObject。

尝试修改您的JSON,以#34; {&#34;并以&#34;}&#34;结束,使根成为对象而不是数组。