2016-08-22 11 views
0

他の投稿でこの問題を探していましたが、解決できません。Retrofit 2 "BEGIN_ARRAYが期待されていましたが、1行目の2列目のパスが" BEGIN_OBJECTでした。 "

フォーマットJSONレスポンス:レトロフィット2

@GET("products") 
    Call<List<ProductTest>> getProductList(); 

@GET("products/{product}") 
Call<ProductTest> getProduct(@Path("product") int product); 

@GET("success") 
Call<SuccessTest> getSuccessNb(); 

JAVA製品クラス

public class ProductTest { 
    String name; 
    String description; 

    public String getName() { 
     return name; 
    } 

    public String getDescription() { 
     return description; 
    } 
} 

レトロフィット2ビルダー

Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("http://10.0.2.2:80/App/PHP_script/reading_all_products.php/") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
CatalogAPI service = retrofit.create(CatalogAPI.class); 
ため

{ 
    "products":[ 
     { 
     "name":"Top fluide doubl\u00e9", 
     "description":"DESCRIPTION ........" 
     }, 
     { 
     "name":"Sweat avec fentes lat\u00e9rales", 
     "description":"DESCRIPTION ........" 
     }, 
     { 
     "name":"Robe \u00e9paules d\u00e9nud\u00e9es", 
     "description":"DESCRIPTION ........" 
     }, 
     { 
     "name":"Blouson bomber \u00e0 zip imprim\u00e9", 
     "description":"DESCRIPTION ........" 
     } 
    ], 
    "success":4 
} 

APIリスト内の製品のリストまたは1つの製品を得るために働いていないJSONレスポンス

service.getSuccessNb().enqueue(new Callback<SuccessTest>() { 
      @Override 
      public void onResponse(Call<SuccessTest> call, Response<SuccessTest> response) { 
       if(response.isSuccessful()) 
       { 
        Log.i("retrofit : ", "" + response.body().getSuccess()); 
       } 
       else 
       { 
        Log.i("retrofit : ", "NULL BODY -> " + response.errorBody()); 
       } 
      } 

      @Override 
      public void onFailure(Call<SuccessTest> call, Throwable t) { 
       Log.i("retrofit : ", "onFailure -> " + t.getLocalizedMessage()); 

      } 
     }); 

サービスの成功を得るために取り組んでいる

サービス

service.getProductList().enqueue(new Callback<List<ProductTest>>() { 
      @Override 
      public void onResponse(Call<List<ProductTest>> call, Response<List<ProductTest>> response) { 
       Log.i("retrofit : ", "pre-ok"); 

       if(response.isSuccessful()) 
       { 
        Log.i("retrofit : ", "ok"); 
       } 
       else 
       { 
        Log.i("retrofit : ", "NULL BODY -> " + response.errorBody()); 
       } 
      } 

      @Override 
      public void onFailure(Call<List<ProductTest>> call, Throwable t) { 
       Log.i("retrofit : ", "onFailure -> " + t.getLocalizedMessage()); 
      } 
     }); 

     service.getProduct(2).enqueue(new Callback<ProductTest>() { 
      @Override 
      public void onResponse(Call<ProductTest> call, Response<ProductTest> response) { 
       if(response.isSuccessful()) 
       { 
        Log.i("retrofit : ", "" + response.message()); 
        Log.i("retrofit : ", response.body().getName() + " => " + response.body().getDescription()); 
       } 
       else 
       { 
        Log.i("retrofit : ", "NULL BODY -> " + response.errorBody()); 
       } 
      } 

      @Override 
      public void onFailure(Call<ProductTest> call, Throwable t) { 
       Log.i("retrofit : ", "onFailure -> " + t.getLocalizedMessage()); 
      } 
     }); 

アン最後の2つのケースでエラーが表示されます。

予想されるBEGIN_ARRAYは、1行目の2列目の$でBEGIN_OBJECTでした。

は、私は完璧に働いているラッパークラスを使用しているが、私は完全にレトロフィット2とJSON

間の通信

答えて

0

の着信JSONを理解するために、この例のためのいくつかの説明をしたい

{ <- indicates its a object 
    "products":[ <- this object has a field called products and its value is array 

プロダクトとしてフィールドの1つを持つオブジェクトが存在し、このフィールドがリストであることを示します。

class ProductsResponse { 
List<Products> products; 
私がやったこと
+0

が、それは働いて、なぜ私は理解していない... –

+0

チェックの更新。これは、あなたがJSON形式に – UDI

+0

感謝を理解するのに役立ちますが、私はの1つのオブジェクトのみを取得することができますどのように願っています改造を伴うアレイ「製品」? –

関連する問題