预期 BEGIN_ARRAY 但 Gson 和 Retrofit BEGIN_OBJECT

Expected BEGIN_ARRAY but was BEGIN_OBJECT with Gson and Retrofit

我的应用尝试使用 RetrofitGson 消耗 API。

我的请求是 https://nahuatiliztli.herokuapp.com/laws.json 它 returns 返回:

[{"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"}]

我的代码结构如下…

我的模型是Lawclass:

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 数组,这确实是我想要的,但我还不知道如何让它工作。

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

在你的 Activity 中:

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方法。