com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 1 列路径 $ 处为 STRING

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

我收到以下错误

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

还有其他一些解决方案,但 none 对我有用。 大多数人似乎都在谈论必须将 Call<WordOfTheDay> 变成 Call<List<WordOfTheDay>>,但由于 json 不是无用的数组。 我相信是 JSON,但不确定。

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"}]}

改造 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();
        }
    });

界面

public interface WordAPI {

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

}

您可能在第一个 '{' 之前有不可见的字符?因为错误说第一个元素是字符串而不是对象,所以如果真实的 json 看起来像:@#${"id":...

和我一样的问题。 终于找到了根本原因,请确保请求的URI是正确的。