1
votes

I'm trying to use retro fit with some json but can't see what's wrong with my json string, i keep getting the same error.

I'm expecting an list of word objects but ive missed something? Any ideas?

retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

JSON:

{
    "words": [
        {
            "id": "1",
            "word": "submarine",
            "word_syllables": "sub-mar-ine",
            "picture": "none.jpg",
            "picture_dir": "",
            "soundfile": "",
            "user_id": "1",
            "created": "2015-04-08 16:32:07",
            "modified": "0000-00-00 00:00:00"
        },
        {
            "id": "2",
            "word": "computer",
            "word_syllables": "com-pute-r",
            "picture": "computer.jpg",
            "picture_dir": "",
            "soundfile": "",
            "user_id": "0",
            "created": "2015-04-08 16:32:07",
            "modified": "0000-00-00 00:00:00"
        }
    ]
}

Retrofit Android code:

private void requestData(){

    RestAdapter adapter=new RestAdapter.Builder()
    .setEndpoint(ENDPOINT)
    .build();

    WordsAPI api=adapter.create(WordsAPI.class);

    api.getRestWordFeed(new Callback<List<Word>>(){

            @Override
            public void failure(RetrofitError arg0) {
                // TODO Auto-generated method stub
                Log.v("error",arg0.getMessage());
            }

            @Override
            public void success(List arg0, Response arg1) {
                // TODO Auto-generated method stub  
                wordList=arg0;
                printList();                    
            }
    });

}

API interface:

package com.example.testretrofitlib;

import java.util.List;

import retrofit.Callback;
import retrofit.http.GET;

public interface WordsAPI { 

    @GET("/rest_words/index.json")
    public void getRestWordFeed(Callback<List<Word>> response);


}
2
You are expecting a List<Word> but the JSON has an object Word that has an array in it.kmcnamee

2 Answers

3
votes

Your JSON is an object that has one attribute: 'words' which is an array of word objects. Your error: "Expected BEGIN_ARRAY but was BEGIN_OBJECT" is indicating that you are expecting an array, but returned an object. One solution would be to return the following as json:

[
    {
        "id": "1",
        "word": "submarine",
        "word_syllables": "sub-mar-ine",
        "picture": "none.jpg",
        "picture_dir": "",
        "soundfile": "",
        "user_id": "1",
        "created": "2015-04-08 16:32:07",
        "modified": "0000-00-00 00:00:00"
    },
    {
        "id": "2",
        "word": "computer",
        "word_syllables": "com-pute-r",
        "picture": "computer.jpg",
        "picture_dir": "",
        "soundfile": "",
        "user_id": "0",
        "created": "2015-04-08 16:32:07",
        "modified": "0000-00-00 00:00:00"
    }
]
2
votes

The list of words object of that JSON is embedded inside the value of an object with a "words" property. That error just points out that, it was expecting an array delimiter [ instead it found the start of that object.

You could either fix the JSON (removing that {"words": <ACTUAL_ARRAY> } and just leaving the array with two elements) or modify the data structure you expect in getRestWordFeed.