2016-04-13 26 views
2

Bir URL için 2 tür yanıtım var. İlk olarak nesnelerin listesi ve ikinci hata yanıtı nesnesidir.Retrofit 2 ile dinamik JSON nasıl kullanılır?

İlk:

[ 
    { 
     id: 1, 
     title: "title1", 
     description: "", 
     position: 1000 
    }, 

    { 
     id: 2, 
     title: "title3", 
     description: "", 
     position: 1000 
    }, 

    { 
     id: 3, 
     title: "title3", 
     description: "", 
     position: 1000 
    } 
] 

İkincisi:

{ 
    "status":"error", 
    "error":"no token" 
} 

herhangi bir fikir nasıl Recommended 2 ile üstesinden? Sunucu hata yanıtını verir zaman hata var:

Category.class sunucuya

public class Category{ 

    @SerializedName("id") 
    @Expose 
    private Id id; 
    @SerializedName("title") 
    @Expose 
    private String title; 
    @SerializedName("description") 
    @Expose 
    private String description; 
    @SerializedName("position") 
    @Expose 
    private Integer position; 


    /** 
    * 
    * @return 
    * The id 
    */ 
    public Id getId() { 
     return id; 
    } 

    /** 
    * 
    * @param id 
    * The id 
    */ 
    public void setId(Id id) { 
     this.id = id; 
    } 

    /** 
    * 
    * @return 
    * The title 
    */ 
    public String getTitle() { 
     return title; 
    } 

    /** 
    * 
    * @param title 
    * The title 
    */ 
    public void setTitle(String title) { 
     this.title = title; 
    } 

    /** 
    * 
    * @return 
    * The description 
    */ 
    public String getDescription() { 
     return description; 
    } 

    /** 
    * 
    * @param description 
    * The description 
    */ 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    /** 
    * 
    * @return 
    * The position 
    */ 
    public Integer getPosition() { 
     return position; 
    } 

    /** 
    * 
    * @param position 
    * The position 
    */ 
    public void setPosition(Integer position) { 
     this.position = position; 
    } 



} 

İsteği "hattı 1 sütun 2 yol $ at BEGIN_OBJECT Beklenen BEGIN_ARRAY ama oldu":

public void loadCategories(final boolean pullToRefresh) { 

     Call<List<Category>> categoriesCall = mRequestHandler.getCategories(Keys.CATEGORY_CATALOGS); 

     categoriesCall.enqueue(new Callback<List<Category>>() { 
      @Override 
      public void onResponse(Call<List<Category>> call, Response<List<Category>> response) { 

       if (response.isSuccess()) { 
        getView().setData(response.body()); 
       } 
      } 

      @Override 
      public void onFailure(Call<List<Category>> call, Throwable t) { 

       getView().showError(null, pullToRefresh); 
       Log.e("Error:", t.getMessage()); 

      } 

     }); 


    } 
+0

Kodunuzu gönderir misiniz? –

+0

Dizininizde olduğu ve ayrıca hata özelliklerinin bulunduğu bir sarıcı yanıt nesnesi kullanın. Ya bir tane boş olacak, ne tür bir tepki aldığınızı görmek için kontrol edin. Başımın tepesinden, – for3st

+0

yanıtı ile aynı anda dizi ve nesneyi kullanmanın mümkün olup olmadığını bilmiyorum. Yanıt bir dizi veya nesne olamaz. Aksi takdirde, Retrofit olmadan yapmanız ve el ile nesnelerinizi ayrıştırmanız ve bu özel durumu yakalamanız gerekir. –

cevap

0

neyi Görüyorum ki, sunucunuz HTTP_STATUS 200 üzerinde hata gönderiyor. Bu yüzden, retrofit bir yanıt olarak değil, bir hata olarak düşünüyor. Bu değişikliği sunucu tarafında yapmak public void onFailure (Arama> çağrı, Throwable t) çağrısı ile sonuçlanacaktır.