Home > other >  Retrofit Body isn't showing desired response from Nested JSON
Retrofit Body isn't showing desired response from Nested JSON

Time:03-27

Edited in picture for debugI've been trying to connect an Android App to the Fitbit API using Retrofit however I'm struggling with getting a connection to a JSON with a nested user section. I've managed to get the classes set up however get Body: com.example.myapplication.User@6fe68c1 when requesting the body back.

Whilst learning about Retrofit I've had no problems with using however this seems to be different because of the "user" in the JSON.

Shortened JSON I'm working from

{
    "user": {
        "age": 23,
        "avatar": "https://static0.fitbit.com/images/profile/defaultProfile_100.png",
        "averageDailySteps": 2673,
        "dateOfBirth": "1999-01-25",
        "displayName": "Name.",
        "features": {
            "exerciseGoal": true
        },
        "fullName": "Full Name",
        "gender": "MALE",
        "glucoseUnit": "METRIC",
        "height": 180.3,
        "memberSince": "2022-02-28",
        "startDayOfWeek": "MONDAY",
        "strideLengthRunning": 123.10000000000001,
        "weight": 72.5,
    }
}

Fitbit Class

    imports
    
    @Generated("jsonschema2pojo")
    public class Fitbit {
    
        @SerializedName("user")
        @Expose
        private User user;
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
    }

User Class

imports

@Generated("jsonschema2pojo")
public class User {

    @SerializedName("age")
    @Expose
    private Integer age;
    @SerializedName("avatar")
    @Expose
    private String avatar;
    @SerializedName("averageDailySteps")
    @Expose
    private Integer averageDailySteps;
    @SerializedName("dateOfBirth")
    @Expose
    private String dateOfBirth;
    @SerializedName("fullName")
    @Expose
    private String fullName;
    @SerializedName("gender")
    @Expose
    private String gender;
    @SerializedName("height")
    @Expose
    private Double height;
    @SerializedName("memberSince")
    @Expose
    private String memberSince;
    @SerializedName("startDayOfWeek")
    @Expose
    private String startDayOfWeek;
    @SerializedName("strideLengthRunning")
    @Expose
    private Double strideLengthRunning;
    @SerializedName("strideLengthWalking")
    @Expose
    private Double strideLengthWalking;
    @SerializedName("timezone")
    @Expose
    private String timezone;
    @SerializedName("waterUnitName")
    @Expose
    private String waterUnitName;
    @SerializedName("weight")
    @Expose
    private Double weight;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Integer getAverageDailySteps() {
        return averageDailySteps;
    }

    public void setAverageDailySteps(Integer averageDailySteps) {
        this.averageDailySteps = averageDailySteps;
    }

    public String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public String getMemberSince() {
        return memberSince;
    }

    public void setMemberSince(String memberSince) {
        this.memberSince = memberSince;
    }

    public String getStartDayOfWeek() {
        return startDayOfWeek;
    }

    public void setStartDayOfWeek(String startDayOfWeek) {
        this.startDayOfWeek = startDayOfWeek;
    }

    public Double getStrideLengthRunning() {
        return strideLengthRunning;
    }

    public void setStrideLengthRunning(Double strideLengthRunning) {
        this.strideLengthRunning = strideLengthRunning;
    }

    public Double getStrideLengthWalking() {
        return strideLengthWalking;
    }

    public void setStrideLengthWalking(Double strideLengthWalking) {
        this.strideLengthWalking = strideLengthWalking;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

    public String getWaterUnitName() {
        return waterUnitName;
    }

    public void setWaterUnitName(String waterUnitName) {
        this.waterUnitName = waterUnitName;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

}

JsonPlaceholderAPI Interface Class

imports

public interface JsonPlaceholderAPI {

    @Headers({"Authorization: Bearer bearercodeinserted"})
    @GET("https://api.fitbit.com/1/user/-/profile.json")
    Call<User> getUser();
}

MainActivity

public class MainActivity extends AppCompatActivity {
private TextView textViewResult;

private JsonPlaceholderAPI jsonPlaceholderAPI;

@Override
protected void onCreate(Bundle savedInstanceState) {
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textViewResult = findViewById(R.id.text_view_result);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://jsonplaceholder.typicode.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    jsonPlaceholderAPI = retrofit.create(JsonPlaceholderAPI.class);
    getUser();
}

private void getUser() {

    Call<User> call = jsonPlaceholderAPI.getUser();

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {

            if (!response.isSuccessful()) {
                textViewResult.setText("Code: "   response.code());
                return;
            }

            textViewResult.setText("Body: "   response.body());

            }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            textViewResult.setText(t.getMessage());
        }
    });
}

}

CodePudding user response:

Solution:

Change Call<User>() to Call<Fitbit>() and response.body().getUser().toString()

If you want textViewResult.setText("Body: " response.body()); to give you string representation of your User data you have to override toString() function on your User object. For example:

public class User {

    @SerializedName("age")
    @Expose
    ...

    @Override
    String toString() {
       return "age: "   age   " avatar: "   avatar   ....;
    }

}
  • Related