预计BEGIN_ARRAY但是GEG和Retrofit是BEGIN_OBJECT

时间:2016-07-17 06:31:45

标签: android retrofit2 gson

我的应用尝试使用RetrofitGson来使用API​​。

我的请求是https://nahuatiliztli.herokuapp.com/laws.json,然后返回:

[{"id":1,"title":"Constitución Política de los Estados Unidos Mexicanos","url":"http://www.diputados.gob.mx/LeyesBiblio/pdf/1_29ene16.pdf","created_at":"2016-07-10T02:36:00.641Z","updated_at":"2016-07-10T02:36:00.641Z"}]

我的代码结构如下......

我的模型是Law类:

public class Law {
    @SerializedName("id")
    private Integer mId;
    @SerializedName("title")
    private String mTitle;
    @SerializedName("url")
    private String mUrl;

    public Law(Integer id, String title, String url) {
        mId = id;
        mTitle = title;
        mUrl = url;
    }

    public Integer getId() {
        return mId;
    }

    public void setId(Integer id) {
        mId = id;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getUrl() {
        return mUrl;
    }

    public void setUrl(String url) {
        mUrl = url;
    }
}

我的回复抽象是LawsResponse class:

public class LawsResponse {
    private List<Law> mLaws;

    public List<Law> getLaws() {
        return mLaws;
    }

    public void setLaws(List<Law> laws) {
        mLaws = laws;
    }
}

Law模型的服务界面是:

public interface LawsService {
    @GET("https://nahuatiliztli.herokuapp.com/laws.json")
    Call<LawsResponse> listLaws();
}

我的适配器是LawsApiAdapter

public class LawsApiAdapter {

    private static LawsService API_SERVICE;

    public static LawsService getInstance() {
        if (API_SERVICE==null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(buildLawsApiGsonConverter()))
                    .build();

            API_SERVICE = retrofit.create(LawsService.class);
        }

        return API_SERVICE;
    }

    private static Gson buildLawsApiGsonConverter() {
        return new GsonBuilder()
                .registerTypeAdapter(
                        LawsApiAdapter.class,
                        new LawsDeserializer())
                .create();
    }

}

我已尝试在此网站上发布了许多建议但仍然抛出此错误:

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

我理解通过阅读很多帖子,它期待一个Json对象并且它正在接收一个Json数组,确实是我想要的,但我不知道如何让它工作。

1 个答案:

答案 0 :(得分:3)

不需要LawsResponse。更新您的界面

public interface LawsService {
    @GET("https://nahuatiliztli.herokuapp.com/laws.json")
    Call<List<Law>> listLaws();
}

您可以按如下方式添加改装请求:

Call<List<Law>> call = LawsApiAdapter.getInstance().listLaws();
call.enqueue(this);//pass your callback instance

在您的活动中:

public class YourActivity extends Activity implements Callback<List<Law>>{

//your rest of methods

@Override
    public void onResponse(Call<List<Law>> call, Response<List<Law>> response) {
        List<Law> lawlist = response.body();
    }

    @Override
    public void onFailure(Call<List<Law>> call, Throwable t) {

        Log.i("log", "exhibits api error : " + t.getLocalizedMessage());

    }

    private void loadLawList(){
Call<List<Law>> call = LawsApiAdapter.getInstance().listLaws();
call.enqueue(this);//pass your callback instance
    }
} 

如果要加载数据,请调用loadLawList方法。