Good day,
I know this question has been asked and the other two topics I found did help up until a point. I have a response from the client that I do not have control over so the format it comes through is what I need to work with.
The Json contains a list of user info from multiple users. That is the reason I was using a List instead of a POJO. The issue is as follows. the POJO does return without any errors but also without any values and only one value instead of more than one
So what I gather is that the JSON is in a format where it is not seen as a list but an object.
Using POSTMAN as a benchmark tool to verify if the GET call work the values return in list
I am still a noob in this area so any help will be appreciated.
The JASON response from postman looks as follow(That is where I realised the way its being retrieved might be seen as object and not a list.
{
"items": [
{
"userpassword": "TEST123",
"userstatus": "A",
"useremail": "Someperson@domain.com",
"firstname": "Some",
"lastname": "Person"
},
{
"userpassword": "Pass123@",
"userstatus": "C",
"useremail": "personannom@domain.org",
"firstname": "Annom",
"lastname": "Person`"
},
{
"userpassword": "PietIE2#3",
"userstatus": "A",
"useremail": "Piet@pieinthesky.co.uk",
"firstname": "Piet",
"lastname": "Pompies"
},
],....
}
So it seems that I need to drill in one more level to get to the values.
Just some more examples
My Interface class:
import retrofit2.Call;
import retrofit2.http.GET;
public interface JasonPlaceHolder {
@GET("allusers")
Call<UserInfo> getUserInfo();"
//Call<List<UserInfo>> getUserInfo(); WAS
}
My class to store the information in
public class UserInfo {
private String userpassword;
private String userstatus;
private String useremail;
private String firstname;
private String lastname;
public String getUserpassword() {
return userpassword;
}
public String getUserstatus() {
return userstatus;
}
public String getUseremail() {
return useremail;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
}
And then my call in the mainactivity class (Because its not a list anymore I cannot loop through the records anymore
Retrofit retrofit = new Retrofit.Builder().
baseUrl("http://10..../com/").
addConverterFactory(GsonConverterFactory.create()).
build();
JasonPlaceHolder jasonPlaceHolderApi = retrofit.create(JasonPlaceHolder.class);
Call<UserInfo> call = jasonPlaceHolderApi.getUserInfo();
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
if(!response.isSuccessful()){
textVierResults.setText("code: " + response.code());
return;
}
UserInfo userInfos = response.body();
//for(UserInfo ui:userInfos){
String content = "";
content += "Useremail: " + userInfos.getUseremail() + "\n";
content += "Userpassword: " + userInfos.getUserpassword() + "\n";
content += "Firstname: " + userInfos.getFirstname() + "\n";
content += "Lastname: " + userInfos.getLastname() + "\n";
content += "Userstatus: " + userInfos.getUserstatus() + "\n";
textVierResults.append(content);
// }
}
@Override
public void onFailure(Call<UserInfo> call, Throwable t) {
Toast.makeText(MainActivity.this, "An error occurred", Toast.LENGTH_SHORT).show();
textVierResults.setText(t.getMessage());
}
});
Could anybody suggest a solution.