2017-08-26 4 views
0

Je suis nouveau à Retrofit. Voici mon code pour obtenir la liste des fournisseurs

Exemple de réponse JSON est un tableau JSON & pas d'objet JSON

[ 
{ 
"Key" : "1", 
"Value" : "xyz" 
}, 
{ 
"Key" : "2", 
"Value" : "abc" 
} 
] 

Voici mon code

private void callToRetrofit() { 
    ApiInterface apiInterface = ApiClient.getApiClient() 
      .create(ApiInterface.class); 
    Call<List<SD_Checklist_Supplier_Model>> call = apiInterface.getVendors(); 
    call.enqueue(new Callback<List<SD_Checklist_Supplier_Model>>() { 
     @Override 
     public void onResponse(Call<List<SD_Checklist_Supplier_Model>> call, Response<List<SD_Checklist_Supplier_Model>> response) { 
      Log.v("onResponse", " : ok" + response == null ? " null" : "okkkk" + response.toString()); 
     } 

     @Override 
     public void onFailure(Call<List<SD_Checklist_Supplier_Model>> call, Throwable t) { 
      Log.v("onFailure", " : " + t.toString()); 
     } 
    }); 
} 

Modèle classe

public class SD_Checklist_Supplier_Model { 

    @SerializedName("Key") 
    private String supplierID; 
    @SerializedName("Value") 
    private String supplierName; 
    private boolean isSupplierSelected; 

    public String getSupplierID() { 
     return supplierID; 
    } 

    public void setSupplierID(String supplierID) { 
     this.supplierID = supplierID; 
    } 

    public String getSupplierName() { 
     return supplierName; 
    } 

    public void setSupplierName(String supplierName) { 
     this.supplierName = supplierName; 
    } 

    public boolean isSupplierSelected() { 
     return isSupplierSelected; 
    } 

    public void setSupplierSelected(boolean supplierSelected) { 
     isSupplierSelected = supplierSelected; 
    } 
} 

Actuellement , Je reçois

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT attendu, mais était BEGIN_ARRAY à la ligne 1 colonne 2 chemin $

S'il vous plaît aidez-moi.

+0

Vérifiez votre API POSTMAN. Ça doit venir dans un objet qui n'est pas dans le tableau. Comme mention d'erreur. – sushildlh

+0

Comme je l'ai déjà mentionné. Il est json tableau pas standard objet json à l'intérieur – VVB

+0

allez-vous partager l'URL de l'API? – sushildlh

Répondre

0

Votre réponse à l'appel de l'API est une liste que vous n'attribuez pas à une liste.

Changer votre code

Call<SD_Checklist_Supplier_Model> call = apiInterface.getVendors(); 

comme

Call<List<SD_Checklist_Supplier_Model>> call = apiInterface.getVendors(); 
+0

J'ai essayé cela mais après avoir fait cet appel api ne fonctionne pas – VVB

+0

Pouvez-vous montrer votre classe 'SD_Checklist_Supplier_Model' –

+0

S'il vous plaît voir édité à la question – VVB