简体   繁体   中英

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

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

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/

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:

{"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. response.body() would include help and the result would be it's sub-element. 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.

The response, which you are getting from the API, doesn't fit the Result POJO.

The response you get from API is like below:

{
  "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. So, just create a POJO which fairly represents the actual response.

{
    "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:

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

Then, finally you can do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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