Reputation: 33
I have this error, it's about an object.
This is my port:
http://localhost:3000/cupones
I can see this:
[
{
"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"
}
]
Then I created a class with jsonschema2pojo, after that I created my JSONResponse, here:
public class JSONResponse {
private Cupon[] cupon;
public Cupon[] getCupon() {
return cupon;
}
}
I think that I can change something here in my interface that is called
public interface RequestInterface {
@GET("cupones")
Call<JSONResponse> getJSON();
}
Finally in my 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());
}
});
}
How could I fix that?
Upvotes: 1
Views: 7663
Reputation: 785
I faced the same problem because my Json data start with array. Changing the response data to a List solves the problem.
Api Interface :
public interface MainDataApiInterface {
@GET("gunzaramfuni.php")
Call<List<MainData>> getSampleDataList();
}
Network call:
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);
}
});
}
}
Upvotes: 0
Reputation: 8237
According to your problem
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path
We can know that your parse json error ,and your root tag of your response is []
(JSONArray
).So you should use List
in your Retrofit
.
So you should change JSONResponse
to List<JSONResponse>
in your code .
First
change the JSONResponse
class
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;
}
}
Second
change the call code Call<JSONResponse> getJSON();
to
@GET("cupones")
Call<List<JSONResponse>> getJSON();
Upvotes: 3