[英]Retrofit returning null response Android
我正在使用Retrofit進行API調用,處理響應時會收到下一個錯誤(需要從API調用中獲取數據)-
嘗試在空對象引用上調用接口方法'java.lang.Object java.util.List.get(int)'
我不知道我是否做對了。 無論如何,這是我的代碼。
這是網址鏈接: https : //data.gov.il/api/
改造電話 -
@GET("datastore_search?resource_id=2c33523f-87aa-44ec-a736-edbb0a82975e")
Call<Result> getRecords();
加裝基礎通話 -
private static Retrofit retrofit;
public static final String BASE_URL = "https://data.gov.il/api/action/";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
模型類 -
public class Result {
@SerializedName("include_total")
@Expose
private Boolean includeTotal;
@SerializedName("resource_id")
@Expose
private String resourceId;
@SerializedName("fields")
@Expose
private List<Field> fields = null;
@SerializedName("records_format")
@Expose
private String recordsFormat;
@SerializedName("records")
@Expose
private List<Record> records = null;
@SerializedName("limit")
@Expose
private Integer limit;
@SerializedName("_links")
@Expose
private Links links;
@SerializedName("total")
@Expose
private Integer total;
public Boolean getIncludeTotal() {
return includeTotal;
}
public void setIncludeTotal(Boolean includeTotal) {
this.includeTotal = includeTotal;
}
public String getResourceId() {
return resourceId;
}
public void setResourceId(String resourceId) {
this.resourceId = resourceId;
}
public List<Field> getFields() {
return fields;
}
public void setFields(List<Field> fields) {
this.fields = fields;
}
public String getRecordsFormat() {
return recordsFormat;
}
public void setRecordsFormat(String recordsFormat) {
this.recordsFormat = recordsFormat;
}
public List<Record> getRecords() {
return records;
}
public void setRecords(List<Record> records) {
this.records = records;
}
...
主要活動 -
RecallService service = RetrofitClientInstance.getRetrofitInstance().create(RecallService.class);
Call<Result> records = service.getRecords();
records.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
Log.d(TAG, String.valueOf(response.body().getRecords().get(0).getId())); // ERROR
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.e(TAG, t.getMessage());
}
});
您共享的API鏈接以以下格式返回響應:
{"help": "https://data.gov.il/api/3/action/help_show?name=datastore_search", "success": true, "result": {...}}
您將響應對象設置為Result類型,它實際上是json響應中root元素help
中的一個子元素。 response.body()
將包含help
, result
將是它的子元素。 由於未正確解析,因此您得到的響應為空。
您將需要在模型類中包括根元素,並更新API調用以將該類類型用作響應類型。
您從API獲得的響應不符合Result
POJO。
您從API獲得的響應如下:
{
"help": "https://data.gov.il/api/3/action/help_show?name=datastore_search",
"success": true,
"result": {...}
}
通過使用Result
POJO,您假設您獲得了如下所示的響應,它是實際響應json中的json,而不是您實際收到的響應。 因此,只需創建一個POJO即可公平地表示實際響應。
{
"include_total": true,
"resource_id": "2c33523f-87aa-44ec-a736-edbb0a82975e",
"fields": [...],
"records_format": "objects",
"records":[...]
}
嘗試制作如下所示的類(自行設置注釋):
class Resp{
Result result;
}
用Resp
替換Result
類,如下所示和其他用法:
@GET("datastore_search?resource_id=2c33523f-87aa-44ec-a736-edbb0a82975e")
Call<Resp> getRecords();
然后,最后您可以執行以下操作:
response.body().getResult().getRecords()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.