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 $

查看:6387
本文介绍了com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第1列为STRING $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但是STRING在第1行第1列路径

还有其他一些解决方案,但没有一个适合我。
大部分似乎都在讨论必须将调用< WordOfTheDay> 变为呼叫< List< WordOfTheDay>> ,但是由于json不是无法帮助的数组。
我相信这是JSON,但我不确定。



JSON

  {id:520687,word:imbricated,contentProvider:{name:wordnik,id:711},note:来自拉丁词imbricātus,覆盖着屋顶瓦,从imbrex,imbric-,瓦,imber,imbr-,雨。,publishDate:2015-11-30T03:00:00.000 + 0000,例子:[{url:http://api.wordnik.com/v4/mid/ae14ef37c95d853a3ccc48d6590a9e1875a7e0920882657447b9dd4443c5d17553ca0c76787ecd04d0559596157a208c,text:因为它流动,它采取的叶子或藤蔓的形式,使堆浆状浆液深入一英尺或更深,并且当你向下看时,它们类似于一些地衣的沾染了叶状和鳞状的叶状体;或者你会想起珊瑚,豹子或鸟的脚,脑或肺或肠的珊瑚, ,标题:Walden,或树林里的生活,id:930392764},{url:http://amzn.to/1P1n9xM,text: 推开门,他标题:Paradiso,JoséLezama Lima,id:0},{url:http:// amzn。 to / 1I938NP,文字:Subaruns的表皮鳞片闪闪发光,像鳞片状的盔甲:喝着炎热的蓝色Pleiadean阳光的生物光电池。,标题:Galactic North,Alastair Reynolds,id:0}], 定义:[{text:重叠,如鳞片或屋顶瓦片;交织在一起,partOfSpeech:形容词,来源:wiktionary}]} 

Retrofit 2.0

  Retrofit retrofit = new Retrofit.Builder()
.baseUrl(MainActivity.BASE_URL)
。客户端(客户端)
.addConverterFactory(GsonConverterFactory.create())
.build();

//初始异步网络调用测试
WordAPI apiService = (WordAPI.class);
Call< WordOfTheDay> call = apiService.getWordOfTheDay();
call.enqueue(new Callback< WordOfTheDay>(){

@覆盖
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.printStack跟踪();
}
});

界面

  public interface WordAPI {

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

}

解决方案您可能在第一个'{'之前有隐形字符?因为错误表示第一个元素是字符串而不是对象,这可能是真正的json的样子:@#$ {id:...


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();

}

解决方案

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":...

这篇关于com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第1列为STRING $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆