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

时间:2017-02-17 17:26:46

标签: android json retrofit2

我正在使用改版从api获取json数据。但是当我运行app时它显示错误com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $。我不知道问题是什么。这是我的json数据:

{"status":true,"message":"Notifications fetched.","data":
 [{"id":"69","type":"liked","text":"Sandip Ghosh liked your photo.","for_userid":"56","from_userid":"55","for_image":"54","seen":"0",
"username":"sandip","firstname":"Sandip","lastname":"Ghosh","imgname":""},
{"id":"64","type":"liked","text":"Sandip Ghosh liked your 
photo.","for_userid":"56","from_userid":"55","for_image":"54","seen":"0","userna
me":"sandip","firstname":"Sandip","lastname":"Ghosh","imgname":""}]}

和我的界面类:

  public static final String BASE_URL = "http://chikoop.com/api/index.php/";
    private static Retrofit retrofit = null;

   static Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }

2 个答案:

答案 0 :(得分:0)

您的json数据看起来有些问题。在导致问题的数据之间放置了很少的新行字符。对于前者,"text":"Sandip Ghosh liked your photo."之后会出现一个新行字符。下面是有效的json。您可以检查json here.

的有效性
{
"status": true,
"message": "Notifications fetched.",
"data": [{
    "id": "69",
    "type": "liked",
    "text": "Sandip Ghosh liked your photo.",
    "for_userid": "56",
    "from_userid": "55",
    "for_image": "54",
    "seen": "0",
    "username": "sandip",
    "firstname": "Sandip",
    "lastname": "Ghosh",
    "imgname": ""
}, {
    "id": "64",
    "type": "liked",
    "text": "Sandip Ghosh liked your photo.",
    "for_userid": "56",
    "from_userid": "55",
    "for_image": "54",
    "seen": "0",
    "username": "sandip",
    "firstname": "Sandip",
    "lastname": "Ghosh",
    "imgname": ""
}]
}

答案 1 :(得分:0)

问题在于API,请尝试调整您的回复。我尝试使用Gson和改装,对我而言,它失败了 或者您可以尝试使用

Okhttp client = new okhttp() Retrofit = new retrofit.builder()。 BaseUrl(您的URL) 。 Setclient(client).addconverterFactory(GsonConverterFactory.create(gson))。构建