2015-11-14 162 views
3

我每次都收到此錯誤:預計begin_array,但是begin_object我不知道是否需要添加反序列化器或warrper類來改造,但我不知道我可以做一個簡單的方法來滿足我所有的項目我的API JSON響應,這是我改裝代碼嘗試:Retrofit 2.0.0 beta2:預計begin_array,但是begin_object

-

的json

{ 
    categoryDetails: [ 

    { 
     id: "1", 
     categoryName: "Fashion" 
    }, 
    { 
     id: "2", 
     categoryName: "Pets" 
    }, 
    { 
     id: "3", 
     categoryName: "Sports" 
    }, 
    { 
     id: "4", 
     categoryName: "Autre1" 
    }, 
    { 
     id: "5", 
     categoryName: "Autre2" 
    } 
    ] 
} 

我的源代碼

import java.util.List; 
import java.io.IOException; 
import java.util.List; 
import android.util.Log; 
import retrofit.Call; 
import retrofit.GsonConverterFactory; 
import retrofit.Retrofit; 
import retrofit.http.GET; 
import retrofit.http.Path; 

public class GitHubClient { 

    public static final String API_URL = "http://192.168.1.22"; 


     public static class Category { 
     public final String categoryName; 
     public final int id; 

     public Category(String categoryName, int id) { 
      this.categoryName = categoryName; 
      this.id = id; 
     } 
     } 
/*-----------------------------------------------------------------------------------------------------------*/ 
     public interface QampaAPI { 
     @GET("/api/category") 
     Call<List<Category>> categoryDetailss(); 
     } 
/*-----------------------------------------------------------------------------------------------------------*/  

     public static void main(String... args) throws IOException { 

      Log.w("retrofit","main()"); 
     // Create a very simple REST adapter which points the GitHub API. 
     Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(API_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

     // Create an instance of our GitHub API interface. 
     QampaAPI apiCall = retrofit.create(QampaAPI.class); 

     // Create a call instance for looking up Retrofit categoryDetailss. 
     Call<List<Category>> call = apiCall.categoryDetailss(); 

     // Fetch and print a list of the categoryDetailss to the library. 
     List<Category> Categorys = call.execute().body(); 

     for (Category Category : Categorys) { 
      Log.w("retrofit",Category.categoryName + " (" + Category.id + ")"); 
     } 


     } 

} 

回答

2

根據您的JSON響應與您創建的類不匹配的錯誤狀態。 嘗試以下POJO類

public static class Category { 
public final List<CatergoryItem> categoryItem; 

    public Category(List<CategoryItem> item) { 
     this.categoryItem = item; 
    } 

    } 
class CategoryItem{ 
public String categoryName; 
    public int id; 
//getter..setter 
} 
+0

仍然給我同樣的錯誤 – Mourad

+0

我仍然有同樣的錯誤這裏是我的代碼:collabedit.com/qbn3p – Mourad

+0

只是使用列表。我的事情會解決你的問題 –

0

你是正確的,你需要一個包裝類來處理外部對象 -

static class CategoryDetails { 
    List<Category> categoryDetails; 
    // add getter and setter 
} 

更新你的界面使用這個對象 -

public interface QampaAPI { 
    @GET("/api/category") 
    Call<CategoryDetails> categoryDetailss(); 
} 

和您的來電 -

Call<CategoryDetails> call = apiCall.categoryDetailss(); 
CategoryDetails details = call.execute().body(); 

您需要訪問details對象內的列表才能遍歷它。

+0

我仍然有同樣的錯誤,這裏是我的代碼:http://collabedit.com/qbn3p – Mourad

+0

同樣的錯誤,當我列表包含單個項目 – amy

相關問題