Reputation: 87
I'm currently using Retrofit 2 to retrieve a JSON Array of Objects. The connection with my URL is successful, but the data is never successfully retrieved as the List is always returned as null. I am unsure why this is occurring; any help would be immensely appreciated, thank you.
JSON Data :
{
"result":{
"data":[
{
"image":"test1",
"text":"text1",
"time":1,
"key":"pm-02"
},
{
"image":"test2",
"text":"test2",
"time":2,
"key":"pm-03"
},
{
"image":"test3",
"text":"test3",
"time":3,
"key":"pm-01"
},
{
"image":"test4",
"text":"test4",
"time":4,
"key":"pm-04"
},
{
"image":"test5",
"text":"test5",
"time":5,
"key":"pm-07"
}
],
"nextKey":6
}
}
Api Interface:
public interface Api {
String BASE_URL = "URL";
@GET("PathtoUrlFunction")
Call<resultList>getResult(@Query("uid")
String myUID, @Query("amount") String
amount);
}
resultList.java
public class resultList {
private static final String TAG = "RESULT LIST";
@SerializedName("data")
@Expose
private List<MessageReference> data;
public List<MessageReference> getResult() {
Log.d(TAG, " STACK OVERFLOW - IN MY RESULT LIST CLASS" + data);
return data;
}
public void setResult(List<MessageReference> temp) { this.data = temp;}
}
MessageReference.java
public class MessageReference {
private String image;
private String text;
private int time;
private String key;
public MessageReference(String image, String text, int time, String key) {
this.text = text;
this.image = image;
this.time = time;
this.key = key;
}
public MessageReference() { }
public String getImage() { return image;
}
public String getText() { return text; }
public int getTime() { return time; }
public String getKey() { return key; }
public void setImage(String image) { this.image = image; }
public void setText(String text) { this.text = text; }
public void setTime(int time) { this.time = time; }
public void setKey(String key) { this.key = key; }
}
SocialMediaTab.Java(Where the call is made)
Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
...
Api api = retrofit.create(Api.class);
Log.d(TAG, "STACK OVERFLOW - API CREATE " + api);
// get result takes in 2 paramters - uid of the user (to retrieve their data), and the amount of objects desired in the returned json array, in this case - 5
Call<resultList> call = api.getResult(loggedinUID, "5");
Log.d(TAG, "STACK OVERFLOW - CALL CREATE " + call);
call.enqueue(new Callback<resultList>() {
@Override
public void onResponse(Call<resultList> call, Response<resultList> response) {
Log.d(TAG, " ON RESPONSE " + call);
resultList result = response.body();
Log.i(TAG, " STACK OVERFLOW - MY CALL" + result.getResult());
}
@Override
public void onFailure(Call <resultList> call, Throwable t) {
Log.i(TAG, "FAILURE " );
t.printStackTrace();
}
});
}
LogCat (in files above)
D/Social Media Tab: STACK OVERFLOW - API CREATE retrofit2.Retrofit$1@131b108
D/Social Media Tab: STACK OVERFLOW - CALL CREATE retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall@d24c09b
D/RESULT LIST: STACK OVERFLOW - IN MY RESULT LIST CLASSnull
I/Social Media Tab: STACK OVERFLOW - MY CALLnull
Upvotes: 0
Views: 1440
Reputation: 659
I think there is problem in your response pojo class. The pojo class should be an object with an object inside it, having jsonarray.Below is a rough code.
public class ApiResponse {
@SerializedName("result")
Result result;
}
public class Result{
@SerializedName("data")
List<MessageReference> list;
}
public class MessageReference{
String image;
String text;
int time;
String key;
}
Upvotes: 1
Reputation: 670
I think their is problem in your resultList class. You are using "data" instead of calling "result" in your model class.
The expected list is in result not directly data.
Your model class should be like this :
Result class
public class Result {
@SerializedName("data") @Expose private List<Datum> data = null; @SerializedName("nextKey") @Expose private Integer nextKey;
public List<Datum> getData() { return data; }
public void setData(List<Datum> data) { this.data = data; }
public Integer getNextKey() { return nextKey; }
public void setNextKey(Integer nextKey) { this.nextKey = nextKey; }
}
Datum Class :
public class Datum {
@SerializedName("image")
@Expose
private String image;
@SerializedName("text")
@Expose
private String text;
@SerializedName("time")
@Expose
private Integer time;
@SerializedName("key")
@Expose
private String key;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
Example class :
public class Example {
@SerializedName("result")
@Expose
private Result result;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
And your ApiInterface Should be like this :
public interface Api {
String BASE_URL = "URL";
@GET("PathtoUrlFunction")
Call<Result>getResult(@Query("uid")
String myUID, @Query("amount") String
amount);
}
Hope this will help. Let me know if their is any issue regarding this!!
Happy coding!
Upvotes: 1