简体   繁体   English

改型返回空响应Android

[英]Retrofit returning null response Android

I'm using Retrofit to make API call, When I handle the response I get the next error (Need to get the data from the API call) - 我正在使用Retrofit进行API调用,处理响应时会收到下一个错误(需要从API调用中获取数据)-

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference 尝试在空对象引用上调用接口方法'java.lang.Object java.util.List.get(int)'

I don't know if I'm doing it right. 我不知道我是否做对了。 Anyway here's my code. 无论如何,这是我的代码。

Here's the url link: https://data.gov.il/api/ 这是网址链接: https : //data.gov.il/api/

Retrofit call - 改造电话 -

@GET("datastore_search?resource_id=2c33523f-87aa-44ec-a736-edbb0a82975e")
Call<Result> getRecords();

Retrofit base call - 加装基础通话 -

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;
}

Model class - 模型类 -

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;
}

...

Main Activity - 主要活动 -

    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());

        }
    });

The API link you've shared returns the response in the format below: 您共享的API链接以以下格式返回响应:

{"help": "https://data.gov.il/api/3/action/help_show?name=datastore_search", "success": true, "result": {...}}

You are setting the response object to be of type Result which is actually a sub-element within the root element help in the json response. 您将响应对象设置为Result类型,它实际上是json响应中root元素help中的一个子元素。 response.body() would include help and the result would be it's sub-element. response.body()将包含helpresult将是它的子元素。 Since it is not parsed correctly, you're getting a null response. 由于未正确解析,因此您得到的响应为空。

You will need to include the root element in your model class and update the API call to use that class type as the response type. 您将需要在模型类中包括根元素,并更新API调用以将该类类型用作响应类型。

The response, which you are getting from the API, doesn't fit the Result POJO. 您从API获得的响应不符合Result POJO。

The response you get from API is like below: 您从API获得的响应如下:

{
  "help": "https://data.gov.il/api/3/action/help_show?name=datastore_search",
  "success": true,
  "result": {...}
}

By using the Result POJO, you are assuming that you get the response as below, which is a json inside the actual response json, and is not what you actually receive. 通过使用Result POJO,您假设您获得了如下所示的响应,它是实际响应json中的json,而不是您实际收到的响应。 So, just create a POJO which fairly represents the actual response. 因此,只需创建一个POJO即可公平地表示实际响应。

{
    "include_total": true,
    "resource_id": "2c33523f-87aa-44ec-a736-edbb0a82975e",
    "fields": [...],
    "records_format": "objects",
    "records":[...]
}

Try making a class like below (set the annotations yourself): 尝试制作如下所示的类(自行设置注释):

class Resp{
    Result result;
}

Replace the class Result with Resp , like below and other usages: Resp替换Result类,如下所示和其他用法:

@GET("datastore_search?resource_id=2c33523f-87aa-44ec-a736-edbb0a82975e")
Call<Resp> getRecords();

Then, finally you can do: 然后,最后您可以执行以下操作:

response.body().getResult().getRecords()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM