I have been following other answers but there is a missing step that i cant find which is resulting in call being successful but the data not being parsed correctly because the first call i make returns a list of objects but only 1 object is returned which is all null
MyModel.java
public class MyModel {
@SerializedName("url")
private String mUrl;
@SerializedName("name")
private String mName;
@SerializedName("description")
private String mDescription;
}
MyModelDeserializer.java
This just checks if its array or object and will simply return the array
public class MyModelTypeAdapter implements JsonDeserializer<ArrayList<MyModel>>{
@Override
public ArrayList<MyModel> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ArrayList<MyModel> objects = new ArrayList<>();
if(json.isJsonArray()){
for(JsonElement e : json.getAsJsonArray()){
objects.add((MyModel)context.deserialize(e,MyModel.class));
}
}else if(json.isJsonObject()){
objects.add((MyModel)context.deserialize(json,MyModel.class));
}
return objects;
}
}
Some other stuff
Gson gson = new GsonBuilder()
.registerTypeAdapter(new TypeToken<ArrayList<MyModel>>() {}.getType(), new MyModelTypeAdapter())
.create();
restAdapter = new RestAdapter.Builder()
.setEndpoint(BuildConstants.BASE_URL)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
This is the part which confusing me, what do i put as the return type of the callback
@GET(URLConstants.LIST_URL)
void getData(Callback<ArrayList<MyModel>> callback);
Edit JSON data
{
"places": [
{
"url": "www.google.com",
"name": "Google",
"description": "Search engine"
},
{
"url": "www.Facebook.com",
"name": "Facebook",
"description": "Social Network"
},
{
"url": "www.amazon.com",
"name": "Amazon",
"description": "Shopping"
}
]
}