android - 应为 BEGIN_OBJECT 但在第 1 行第 2 列路径处为 BEGIN_ARRAY

标签 android arrays json object retrofit2

我有这个错误,它是关于一个对象的。

这是我的端口:

http://localhost:3000/cupones

我可以看到这个:

[
{
    "id": "YLabhnB",
    "empresa": "KFC",
    "titulo": "Mega Online",
    "descripcion": "9 Piezas de pollo, 1 Papa Familiar, 6 Nuggets, 1 Ensalada Familiar, 1 Gaseosa de 1.5Lts",
    "precio": 55.9,
    "imgUrl": "https://www.kfc.com.pe/Imagenes/800x275/BOTONERA%20MEGA%20ONLINE%202.jpg"
},
{
    "id": "LLzwhnA",
    "empresa": "Bembos",
    "titulo": "Promo Clásica",
    "descripcion": "Clásica Mediana + 1 Papa Mediana + 1 Gaseosa 500ml",
    "precio": 13.9,
    "imgUrl": "http://www.bembos.com.pe/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/o/k/okpromoclasica__2_.png"
}
]

然后我用 jsonschema2pojo 创建了一个类,之后我在这里创建了我的 JSONResponse:

public class JSONResponse {
    private Cupon[] cupon;

    public Cupon[] getCupon() {
        return cupon;
    }
}

我认为我可以在我的界面中更改一些名为

的东西
public interface RequestInterface {
    @GET("cupones")
    Call<JSONResponse> getJSON();
}

最后在我的 fragment 中:

private void initViews(View rootView){
    recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(new CuponAdapter(new ArrayList<Cupon>()));
    loadJSON();
}

private void loadJSON(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.1.42:3000/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getCupon()));
            adapter = new CuponAdapter(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

我该如何解决?

最佳答案

根据你的问题

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path

我们可以知道你的parse json error,你的response root tag是[] ( JSONArray ). 所以你应该使用 List在你的Retrofit .

所以你应该改变JSONResponseList<JSONResponse>在你的代码中。

首先

更改JSONResponse

public class JSONResponse {

    /**
     * id : YLabhnB
     * empresa : KFC
     * titulo : Mega Online
     * descripcion : 9 Piezas de pollo, 1 Papa Familiar, 6 Nuggets, 1 Ensalada Familiar, 1 Gaseosa de 1.5Lts
     * precio : 55.9
     * imgUrl : https://www.kfc.com.pe/Imagenes/800x275/BOTONERA%20MEGA%20ONLINE%202.jpg
     */

    private String id;
    private String empresa;
    private String titulo;
    private String descripcion;
    private double precio;
    private String imgUrl;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmpresa() {
        return empresa;
    }

    public void setEmpresa(String empresa) {
        this.empresa = empresa;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public double getPrecio() {
        return precio;
    }

    public void setPrecio(double precio) {
        this.precio = precio;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }
}

第二

更改调用代码Call<JSONResponse> getJSON();

@GET("cupones")
Call<List<JSONResponse>> getJSON();

关于android - 应为 BEGIN_OBJECT 但在第 1 行第 2 列路径处为 BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47723715/

相关文章:

android 是否可以写保护我使用 StorageAccessFramework 创建的纯文本文件?

java - 如何使用导航组件在 Activity 中并排显示 2 个 fragment

c# - LINQ 查询字符串[] 按多个匿名列分组

java - 构造函数和用户输入遇到问题

java - jackson - 不要序列化惰性对象

java - 接收 JSON 响应 (android)

javascript - JSON 字符串在 post 请求期间发生更改

android - Kotlin Elvis Operator 在 JsonElement 上失败?

android - Facebook android 集成中的注销功能禁用

ios - 在 swift 中读取并保存在数组中设置数组