0

How can i parse this using retrofit? i'm getting the error BEGIN_OBJECT but was BEGIN_ARRAY

Right now, i'm parsing it this way..

Below is the adapter class

 public static RetroInterface getCommonPathInterface() {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("myurl")
                .build();
        records= restAdapter.create(RetroInterface.class);
        return records;
    }

Below is the interface, RetroInterface.class

 @GET("/mypath")
        void getValue(

                Callback<MyBean> callback    
        );

This is how i call it in the main activity

 getCommonPathInterface().getValue(new Callback<MyBean>() {
@Override
                    public void success(MyBean myBean, Response response) {
                       inti = 0;

                    }

                    @Override
                    public void failure(RetrofitError error) {
                        int i = 0;
                    }

                });

Below is the json response

 [
    {
    id: "111",
    name: "Val1"
    },
    {
    id: "222",
    name: "Val2"
    }
    ]
6
  • How do you parse it at the moment? Commented Apr 4, 2015 at 8:38
  • 1
    please share your current json parsing code block. otherwise nobody can help you. From error message, it is very clear it is expecting a JSON Object as the root object, but it is getting a JSON array as the root one. Commented Apr 4, 2015 at 8:41
  • Take a look :). I've edited the code Commented Apr 4, 2015 at 8:52
  • What are you used for parsing the JSON. Retrofit uses Gson by default to convert HTTP bodies to and from JSON Commented Apr 4, 2015 at 8:59
  • Yup, its the default retrofit GSON. As of now i ain't doing anything from my side. Have used around 9 service calls, all worked fine...except for this JSON response. Commented Apr 4, 2015 at 9:03

3 Answers 3

2

Yippi ! Got it working. Very simple solution. A small change in my callback method.

Instead of Callback<MyBean> callback used Callback<MyBean[]> callback. Problem solved ! :)

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

1 Comment

Wow, it was simple and didn't require a server change. Great! Tanks Abhilash!
1

Hi Right now you are parsing the response as if it was formatted like this:

{
   "contacts":[
      {
         "id":"111",
         "name":"Val1"
      },
      {
         "id":"222",
         "name":"Val2"
      }
   ]
}

The exception tells you this in that you are expecting an object at the root but the real data is actually an array. This means you need to change the type to be an array to JSON object.

Thank you.

4 Comments

You mean to say that i need to change the server side code in such a way that instead of a JSON array, it should start the response with a JSON object, right? Can i do something on the client side?
Yes you can parse whatever server send you if it is in valid json format.
Yes suppose to you need to change over the server side so it would return the response in json object instead of a JSON array.
Thanks Bhavdip. But if possible can something be done via GSON converter, etc? I'm trying to avoid the server thing right now.
0

I'm new to retrofit, but I was having this issue when I was dealing with a multi-class POJO and using code designed for a single-class POJO. Let me know if that's the case. Also, please paste a bit of code! :)

1 Comment

I think, its not a nice answer but definitely it would be a very nice comment.

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.