预期BEGIN_OBJECT但在第1行第2列路径处是BEGIN_ARRAY

时间:2017-12-08 23:43:24

标签: 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();
}

最后在我的片段中:

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

我该如何解决?

2 个答案:

答案 0 :(得分:3)

根据你的问题

  

预期BEGIN_OBJECT但在第1行第2栏路径为BEGIN_ARRAY

我们可以知道您的解析json错误,以及您的回复的根标记是[]JSONArray)。因此,您应该在List中使用Retrofit

因此,您应该在代码中将JSONResponse更改为List<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();

答案 1 :(得分:0)

我遇到了同样的问题,因为我的Json数据以数组开头。将响应数据更改为List可以解决问题。

Api界面:

public interface MainDataApiInterface {

@GET("gunzaramfuni.php")
Call<List<MainData>> getSampleDataList();

}

网络电话:

public class NetworkCall implements RemoteApiService {

@Override
public void getMainDataListFromServer(final ResponseCallBack<List<MainData>> callback) { 

    MainDataApiInterface mainDataApiInterface = MainDataApiClient.GetRetrofitMainDataClient()
            .create(MainDataApiInterface.class);

    Call< List<MainData> > call = mainDataApiInterface.getSampleDataList();

    call.enqueue(new Callback<List<MainData>>() {
        @Override
        public void onResponse(Call<List<MainData>> call, Response<List<MainData>> response) {
            List<MainData> mainDataJsonResponse = response.body();
            callback.onSuccess(mainDataJsonResponse);
        }

        @Override
        public void onFailure(Call<List<MainData>> call, Throwable t) {
            callback.onError(t);
        }
    });
}

}