Peter Chappy
Peter Chappy

Reputation: 1179

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

I'm getting the following error

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

There are some other solutions out there, but none have worked for me. Most seem to be talking about having to make Call<WordOfTheDay> into Call<List<WordOfTheDay>>, but since the json is not an array that does not help. I believe it's the JSON, but am not sure.

JSON

{"id":520687,"word":"imbricated","contentProvider":{"name":"wordnik","id":711},"note":"The word 'imbricated' comes from the Latin word imbricātus, covered with roof tiles, from imbrex, imbric-, roof tile, from imber, imbr-, rain.","publishDate":"2015-11-30T03:00:00.000+0000","examples":[{"url":"http://api.wordnik.com/v4/mid/ae14ef37c95d853a3ccc48d6590a9e1875a7e0920882657447b9dd4443c5d17553ca0c76787ecd04d0559596157a208c","text":"As it flows it takes the forms of sappy leaves or vines, making heaps of pulpy sprays a foot or more in depth, and resembling, as you look down on them, the laciniated lobed and imbricated thalluses of some lichens; or you are reminded of coral, of leopards 'paws or birds' feet, of brains or lungs or bowels, and excrements of all kinds.","title":"Walden, or Life in the woods","id":930392764},{"url":"http://amzn.to/1P1n9xM","text":"Pushing the door open, he noticed an inscription on the frame around the imbricated scales: Portae meae tantum regi.","title":"Paradiso, by José Lezama Lima","id":0},{"url":"http://amzn.to/1I938NP","text":"The Subaruns' epidermal scales shimmered like imbricated armour: biological photocells drinking scorching blue Pleiadean sunlight.","title":"Galactic North, Alastair Reynolds","id":0}],"definitions":[{"text":"Overlapping, like scales or roof-tiles; intertwined.","partOfSpeech":"adjective","source":"wiktionary"}]}

Retrofit 2.0

        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(MainActivity.BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    //Initial Async Network Call Test
    WordAPI apiService = retrofit.create(WordAPI.class);
    Call<WordOfTheDay> call = apiService.getWordOfTheDay();
    call.enqueue(new Callback<WordOfTheDay>() {

        @Override
        public void onResponse(Response<WordOfTheDay> response, Retrofit retrofit) {
            int statusCode = response.code();
            WordOfTheDay temp = response.body();
            Log.d("temp","t");
        }

        @Override
        public void onFailure(Throwable t) {
            t.printStackTrace();
        }
    });

Interface

public interface WordAPI {

@GET("/words.json/wordOfTheDay?api_key=??")
Call<WordOfTheDay> getWordOfTheDay();

}

Upvotes: 1

Views: 5795

Answers (2)

Sam
Sam

Reputation: 556

Same issue with me. At last, got the root cause, pls keep sure the requested URI is correct.

Upvotes: 0

Slava Vedenin
Slava Vedenin

Reputation: 60094

May be you have invisible chars before first '{' ? Because error said that first element is string instead of object, it's may be if real json look like: @#${"id":...

Upvotes: 1

Related Questions