改装2“预期BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT”

时间:2016-08-22 22:07:59

标签: android json retrofit retrofit2

我曾经在其他帖子上找过这个问题,但我无法用它们来解决。

格式化JSON响应:

{  
   "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
}

改造2的API

@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;
    }
}

Retrofit 2 builder

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);

正在努力获得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());
            }
        });

最后两种情况显示错误:

  

预计BEGIN_ARRAY但在第1行第2列路径$ BEGIN_OBJECT。

我使用了一个完美运行的包装类,但我想对这些情况做一些解释,以便完美理解Retrofit 2和JSON之间的通信

1 个答案:

答案 0 :(得分:0)

传入的json

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

表示存在一个字段作为产品的对象,该字段是一个列表。

class ProductsResponse {
List<Products> products;