0

I am new to GSON and retrofit, Here is my output model (Just for reference structure),

    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
            }

Here is my output

    [{"Result1":

[{"TotalCount":5}]},

    {"Result2":

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

This is my retrofit code

 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"

Expected BEGIN_OBJECT but was BEGIN_ARRAY"

I have tried replacing RootObject With List < Result2 > since I am only interested in result 2.In that case I am getting the response as null.Please help.

0

1 Answer 1

1

The root of your JSON is an array of Result1 objects, but your Callback method is for a single RootObject.

Try modifying your JSON to start with "{" and end with "}", to make the root an object instead of an array.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response.Is there any way for me to get it work without modifying the output JSON?
Changing your Callback from Callback<RootObject> to Callback<List<RootObject>> will work if the JSON inside the initial square braces matches the definition of RootObject. You'd then look for your RootObject in the List's 0 position.
Only changing it to List<rootobject> works!! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.