Retrofit Body 未显示来自 Nested JSON 的预期响应
Retrofit Body isn't showing desired response from Nested JSON
我一直在尝试使用 Retrofit 将 Android 应用程序连接到 Fitbit API,但是我很难连接到 JSON带有嵌套的用户部分。我已经设法设置了 类,但是在请求正文时得到正文:com.example.myapplication.User@6fe68c1。
在学习 Retrofit 的过程中,我在使用时没有遇到任何问题,但这似乎有所不同,因为 JSON.
中的“用户”
已缩短 JSON 我的工作时间为
{
"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;
}
}
用户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 接口Class
imports
public interface JsonPlaceholderAPI {
@Headers({"Authorization: Bearer bearercodeinserted"})
@GET("https://api.fitbit.com/1/user/-/profile.json")
Call<User> getUser();
}
主要活动
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());
}
});
}
}
解决方案:
将Call<User>()
更改为Call<Fitbit>()
和 response.body().getUser().toString()
如果您希望 textViewResult.setText("Body: " + response.body());
为您提供用户数据的字符串表示,您必须覆盖用户对象上的 toString()
函数。例如:
public class User {
@SerializedName("age")
@Expose
...
@Override
String toString() {
return "age: " + age + " avatar: " + avatar + ....;
}
}
在学习 Retrofit 的过程中,我在使用时没有遇到任何问题,但这似乎有所不同,因为 JSON.
中的“用户”已缩短 JSON 我的工作时间为
{
"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;
}
}
用户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 接口Class
imports
public interface JsonPlaceholderAPI {
@Headers({"Authorization: Bearer bearercodeinserted"})
@GET("https://api.fitbit.com/1/user/-/profile.json")
Call<User> getUser();
}
主要活动
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());
}
});
}
}
解决方案:
将Call<User>()
更改为Call<Fitbit>()
和 response.body().getUser().toString()
如果您希望 textViewResult.setText("Body: " + response.body());
为您提供用户数据的字符串表示,您必须覆盖用户对象上的 toString()
函数。例如:
public class User {
@SerializedName("age")
@Expose
...
@Override
String toString() {
return "age: " + age + " avatar: " + avatar + ....;
}
}