2017-02-17 230 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但語法不正確,你可以寫得更清楚。 –

相關問題