问题描述
我的应用尝试使用Retrofit
和Gson
使用API.
My app try to consume an API using Retrofit
and Gson
.
我的请求是发送给https://nahuatiliztli.herokuapp.com/laws.json
,它返回:
My request is to https://nahuatiliztli.herokuapp.com/laws.json
and it returns back:
[{"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"}]
我的代码结构如下...
My code is structured as follow…
我的模型是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
类:
public class LawsResponse {
private List<Law> mLaws;
public List<Law> getLaws() {
return mLaws;
}
public void setLaws(List<Law> laws) {
mLaws = laws;
}
}
我对Law
模型的服务接口是:
My service interface for Law
model is:
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();
}
}
我尝试了许多在此网站上发布的建议,但仍然引发此错误:
I have tried many suggestions posted in this site but it still throwing this error:
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数组,这确实是我想要的,但是我还不知道如何使它工作.
I understand by reading many posts that it is expecting a Json object and it is receiving a Json array, indeed that I want, but I do not how to make it works yet.
推荐答案
不需要LawsResponse.更新您的界面
LawsResponse is not required. Update your interface
public interface LawsService {
@GET("https://nahuatiliztli.herokuapp.com/laws.json")
Call<List<Law>> listLaws();
}
您可以如下添加您的改装请求:
And you can add your retrofit request as follows:
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方法.
Call loadLawList method when you want to load data.
这篇关于预期为BEGIN_ARRAY,但与Gson和Retrofit一起为BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!