本文介绍了java.lang.IllegalStateException:期望BEGIN_ARRAY,但在第1行第2列是BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Retro Fit 在线连接API。但我在试图解析返回的数据时遇到此错误。

  retrofit.RetrofitError:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:预期的BEGIN_ARRAY,但在BEGIN_OBJECT第1行第2列

返回的数据是在格式和数据模型也在下面给出:

  iGPlaceApi.getStreams(ITEMS_PER_PAGE,pageNumber * ITEMS_PER_PAGE,new Callback< List< mGooglePlacesApiResponse>>(){

@Override
public void success(List< mGooglePlacesApiResponse> mGp,Response response){
int n = mGp.size();
Object asa = mGp.toArray();
}

@Override
public void failure(RetrofitError retrofitError){
String error = retrofitE rror.toString();
}
});

public class mGooglePlacesApiResponse {

public String html_attributions;
// public List< String> html_attributions;
public String next_page_token;
公共列表< place>结果;
}

公开课地点{

public几何几何;
公共字符串图标;
公共字符串ID;
公共字符串名称;
public OpeningHours opening_hours;
公共列表<图片> photo
...


解决方案

更改列表与LT; mGooglePlacesApiResponse> mGp mGooglePlacesApiResponse mGp 。您的JSON包含的对象不是列表。



@edit



顺便说一句,html_attributions是一个数组



@ edit2



好的,我看到你改正了你的html_attrubutions


I am using Retro Fit to connect to API online. But I am getting this error while trying to parse the returned data.

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

The data being returned is in This format and model for data is also given below:

iGPlaceApi.getStreams(ITEMS_PER_PAGE, pageNumber * ITEMS_PER_PAGE, new Callback<List<mGooglePlacesApiResponse>>() {

            @Override
            public void success(List<mGooglePlacesApiResponse> mGp, Response response) {
                int n = mGp.size();
                Object asa = mGp.toArray();
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                String error = retrofitError.toString();
            }
        });

public class mGooglePlacesApiResponse {

    public String html_attributions;
    //public List<String> html_attributions;
    public String next_page_token;
    public List<place> results;
}

public class place {

    public Geometry geometry;
    public String icon;
    public String id;
    public String name;
    public OpeningHours opening_hours;
    public List<Photo> photo
    ...
解决方案

Change List<mGooglePlacesApiResponse> mGp to mGooglePlacesApiResponse mGp. Your JSON contains object not list.

@edit

Btw, html_attributions is an array

@edit2

Well, i see you corrected your html_attrubutions

这篇关于java.lang.IllegalStateException:期望BEGIN_ARRAY,但在第1行第2列是BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 02:35