Android 应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组
我有个错误,是关于一个物体的 这是我的端口:Android 应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组,android,arrays,json,object,retrofit2,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",
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();
}
公共接口请求接口{
@得到(“丘比特”)
调用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());
}
});
}
private void initViews(视图根视图){
recyclerView=(recyclerView)rootView.findViewById(R.id.card\u recycler\u视图);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
setAdapter(新的CuponAdapter(新的ArrayList());
loadJSON();
}
私有void loadJSON(){
改装改装=新改装.Builder()
.baseUrl(“http://192.168.1.42:3000/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface请求=reformation.create(RequestInterface.class);
Call Call=request.getJSON();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
JSONResponse=response.body();
data=newarraylist(Arrays.asList(jsonResponse.getCupon());
适配器=新的铜适配器(数据);
recyclerView.setAdapter(适配器);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“Error”,t.getMessage());
}
});
}
我怎样才能解决这个问题呢?根据您的问题 应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组 我们可以知道您的解析json错误,并且您的响应的根标记是
[]
(JSONArray
)。因此您应该在改型中使用列表
因此您应该将代码中的JSONResponse
更改为List
。
首先
更改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;
}
}
秒
更改调用代码callgetjson()代码>到
@GET("cupones")
Call<List<JSONResponse>> getJSON();
@GET(“丘比特”)
调用getJSON();
我面临同样的问题,因为我的Json数据以数组开头。将响应数据更改为列表可以解决此问题
Api接口:
public interface MainDataApiInterface {
@GET("gunzaramfuni.php")
Call<List<MainData>> getSampleDataList();
public接口maindatapi接口{
@获取(“gunzaramfuni.php”)
调用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);
}
});
}
公共类NetworkCall实现RemoteAPI服务{
@凌驾
public void getMainDataListFromServer(最终响应回调){
MainDataApiInterface MainDataApiInterface=MainDataApiClient.GetReformationMainDataClient()
.create(maindatapiinterface.class);
CallCall=maindatapiinterface.getSampleDataList();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
List maindatjsonresponse=response.body();
onSuccess(maindatjsonresponse);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
回调.onError(t);
}
});
}
}