2017-02-17 143 views
-1

我使用retrofit从api中获取json数据。但是当我运行应用程序时显示错误com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $。我不知道什么的problem.Here是我的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; 
    } 

回答

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": "" 
}] 
} 
+0

没有它的相同,它是由我创建的。我的意思是您提供的json数据与我拥有的相同。 –

+0

的确如数据一样,但无效。您可以在http://jsonlint.com/或http://www.jsoneditoronline.org/查看这两个json。你会知道错误是什么。 – Sanjeet

+0

不,这是正确的另一个谁在同一个项目上工作获取相同的JSON数据。 –

0

的问题是在API中,尝试调整你的回应的有效性。我尝试使用GSON和改造,对我来说壮观 失败或者你可以尝试使用

Okhttp客户端=新okhttp() 改造=新retrofit.builder()。 BaseUrl(您的网址) 。 Setclient(客户端).addconverterFactory(GsonConverterFactory.create(gson))。生成

+0

srry但语法不正确,你可以写得更清楚。 –

相关问题