Search code examples
androidrestpojo

Pojo with unnamed list


I have a link to JSON list and i want to load it in my RecycleView(android). I have done same lists, but them often have a names(List results - example). I have tried to make POJO with list with reffers, but it's not work, although massive of data is loading - in logs.

Image

public class MainActivity extends AppCompatActivity {
  TextView tvName;
  Call<ObjectsListResponse> call;
  private final OAuthService service = RestApi.createService(OAuthService.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item);
    tvName = findViewById(R.id.tvName);

    call = service.getListCities();
    call.enqueue(new Callback<ObjectsListResponse>(){
        @Override
        public void onResponse(Call<ObjectsListResponse> call, Response<ObjectsListResponse> response){
            ObjectsListResponse objectListResponse = response.body();
            tvName.setText(objectListResponse.getAny().get(0).getName());

        }
        @Override
        public void onFailure(Call <ObjectsListResponse> call, Throwable t) {
            if(call.isCanceled()) {
                Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
            }
        }
    });

}

}`

public class ObjectsListResponse {
@SerializedName("")
List<ObjectResponse> any;

public List <ObjectResponse> getAny() {
    return any;
}

}

public class ObjectResponse {
@SerializedName("_entityName")
private String entityName;
@SerializedName("_instanceName")
private String instanceName;
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("version")
private String version;

public String getEntityName() {
    return entityName;
}

public String getInstanceName() {
    return instanceName;
}

public String getId() {
    return id;
}


public String getName() {
    return name;
}

public String  getVersion() {
    return version;
}

}

JSON list without name

JSON list without name

JSON List with Images

JSON List with Images


Solution

  • It looks like your response is a JSON array at the top level, which means that there is no top-level response object. You just have a list of "inner" response objects.

    So you need to delete this class altogether:

    public class ObjectsListResponse {
    
        @SerializedName("")
        List<ObjectResponse> any;
    
        public List <ObjectResponse> getAny() {
            return any;
        }
    }
    

    And change your Call object to be a Call<List> instead. So replace this:

    Call<ObjectsListResponse> call;
    

    and

    call.enqueue(new Callback<ObjectsListResponse>(){
        @Override
        public void onResponse(Call<ObjectsListResponse> call, Response<ObjectsListResponse> response){
            ...
        }
        @Override
        public void onFailure(Call<ObjectsListResponse> call, Throwable t) {
            ...
        }
    });
    

    with this:

    Call<List<ObjectResponse>> call;
    

    and this

    call.enqueue(new Callback<ObjectsListResponse>(){
        @Override
        public void onResponse(Call<List<ObjectResponse>> call, Response<List<ObjectResponse>> response){
            ...
        }
        @Override
        public void onFailure(Call<List<ObjectResponse>> call, Throwable t) {
            ...
        }
    });