2015-11-14 3 views
3

Je reçois à chaque fois cette erreur: attendue begin_array mais était begin_object Je ne sais pas si j'ai besoin d'ajouter un désérialiseur ou une classe warrper pour rénover mais je ne sais pas comment je peux le faire d'une manière facile à monter ma api réponse JSON pour tout mon projet, voici mon code retrofit essayer:Retrofit 2.0.0 beta2: attendu begin_array mais était begin_object

-

mon Json:

{ 
    categoryDetails: [ 

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

Mon code source

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 + ")"); 
     } 


     } 

} 

Répondre

2

Selon l'état de l'erreur, votre réponse JSON ne correspond pas à la classe que vous avez créée. Essayez de suivre la classe 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

Toujours me donner la même erreur – Mourad

+0

j'ai toujours la même erreur ici est mon code: collabedit.com/qbn3p – Mourad

+0

il suffit d'utiliser la liste. Je pense que cela va résoudre votre problème –

0

Vous avez raison, vous avez besoin d'une classe wrapper pour gérer l'objet externe -

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

Mettez à jour votre interface pour utiliser cet objet -

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

et vos appels -

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

Vous devrez accéder à la liste à partir de l'objet details pour parcourir la liste.

+0

j'ai encore la même erreur ici est mon code: http://collabedit.com/qbn3p – Mourad

+0

même erreur pour moi lorsque la liste contient un seul élément – amy