Retrofit2 accessing and internal array of objects

  android, java, retrofit2

I am trying to extract a list of objects from a Json file, However the file begins with an object which contains an array of the objects I require. I am new to retrofit here are my files:
Interface

public interface OSMApi {

    @Headers({"Accept: application/json"})
    @GET("map")
    Call<List<OSMObject>> getOSMObjects(@Query("bbox") String boxMinMaxLatLon);
}

Controller

public class OSMController implements Callback<List<OSMObject>> {

    static final String BASE_URL = "https://api.openstreetmap.org/api/0.6/";
    private static final String TAG = "OSMController";

    private String bboxLatLon = "//latlongshere";

    public void start(){
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    OSMApi osmApi = retrofit.create(OSMApi.class);

    Call<List<OSMObject>> call = osmApi.getOSMNodes(bboxLatLon);
    call.enqueue(this);
}

    @Override
    public void onResponse(Call<List<OSMObject>> call, Response<List<OSMObject>> response) {
        if(response.isSuccessful()){
            List<OSMObject> OSMObjectList = response.body();
            OSMObjectList.forEach(OSMObject -> Log.d(TAG, "onResponse: objects "+OSMObject.getId()));
        }else{
            Log.d(TAG, "onResponse: here : "+response.errorBody() + ", "+response.code());
        }
    }

    @Override
    public void onFailure(Call<List<OSMObject>> call, Throwable t) {
        Log.d(TAG, "onFailure: "+ t.getStackTrace() + ", extra: "+ t.getMessage());
    }
}

The beginning of the Json

{
    "version": "0.6",
    "generator": "CGImap 0.8.3 (652902 spike-07.openstreetmap.org)",
    "copyright": "OpenStreetMap and contributors",
    "attribution": "http://www.openstreetmap.org/copyright",
    "license": "http://opendatacommons.org/licenses/odbl/1-0/",
    "bounds": {
        "minlat": 1.0,
        "minlon": 1.0,
        "maxlat": 1.0,
        "maxlon": 1.0
    },
    "elements": [
        {
            "type": "node",
            "id": 443479273,
            "lat": 1.0,
            "lon": 1.0,
            "timestamp": "2011-04-11T18:06:02Z",
            "version": 3,
            "changeset": 4435725,
            "user": "BeerOfTheTrip",
            "uid": 440880
        },
        {
            "type": "node",
            "id": 443479278,
            "lat": 1.0,
            "lon": 1.0,
            "timestamp": "2011-04-11T18:06:01Z",
            "version": 2,
            "changeset": 4435725,
            "user": "BeerOfTheTrip",
            "uid": 440880
        },
        {
            "type": "node",
            "id": 443479281,
            "lat": 1.0,
            "lon": 1.0,
            "timestamp": "2011-04-11T18:06:01Z",
            "version": 2,
            "changeset": 4435725,
            "user": "BeerOfTheTrip",
            "uid": 440880
        },
etc..........

My Object Pojo

public class OSMObject implements SerializedName {

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("lat")
    @Expose
    private double lat;
    @SerializedName("lon")
    @Expose
    private double lon;
    @SerializedName("timestamp")
    @Expose
    private String timestamp;
    @SerializedName("version")
    @Expose
    private int version;
    @SerializedName("changeset")
    @Expose
    private int changeset;
    @SerializedName("uid")
    @Expose
    private int uid;
    @SerializedName("nodes")
    @Expose
    private int[] nodes;

    //Separation
    private ArrayList<OSMNode> OSMNodes = new ArrayList<>();
    private ArrayList<OSMWay> OSMWays = new ArrayList<>();

    public OSMObject(String type, int id, double lat, double lon, String timestamp, int version, int changeset, int uid, int[] nodes) {
        this.type = type;
        this.id = id;
        this.lat = lat;
        this.lon = lon;
        this.timestamp = timestamp;
        this.version = version;
        this.changeset = changeset;
        this.uid = uid;
        this.nodes = nodes;
    }

    public OSMObject(String type, int id, double lat, double lon, String timestamp, int version, int changeset, int uid) {
        this.type = type;
        this.id = id;
        this.lat = lat;
        this.lon = lon;
        this.timestamp = timestamp;
        this.version = version;
        this.changeset = changeset;
        this.uid = uid;
    }

+getters and setters....

So I do not require any of the initial information in the jsonObject, all I need is a list of the objects in the Elements jsonArray, this consists of two slightly different object one has Nodes[] One doesn’t. hence the 2 constructors in the pojo?

How do I set retrofit up to deal with this? as currently the error I get on failure is:

onFailure: [Ljava.lang.StackTraceElement;@dbe860f, extra: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Source: Android Questions

LEAVE A COMMENT