我有这个错误,是关于一个对象的。
这是我的港口:
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());
}
});
}
我该如何解决?
最佳答案
根据你的问题
预期为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();