在Android项目中使用翻新功能集成API时,发出发布请求时会收到API的null
响应,因此我无法在共享首选项中保存任何数据,但无法当我能够看到响应为null
时,在logcat中找到错误。
活动
Call<UserStatsResponse> call = RetrofitClient.getInstance ().getApi ().userInfo ( token,gender,weight,height,goals,activity,age );
call.enqueue ( new Callback<UserStatsResponse> () {
@Override
public void onResponse(Call<UserStatsResponse> call, Response<UserStatsResponse> response) {
UserStatsResponse userStatsResponse = response.body ();
if(userStatsResponse.isSucess ()){
Log.d("error", String.valueOf ( userStatsResponse ) );
Intent intent = new Intent ( UserStatistics.this, DashboardActivity.class );
intent.setFlags ( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity ( intent );
}
else{
Toast.makeText ( UserStatistics.this, "Some error", Toast.LENGTH_SHORT ).show ();
}
}
@Override
public void onFailure(Call<UserStatsResponse> call, Throwable t) {
Toast.makeText ( UserStatistics.this, t.getMessage (), Toast.LENGTH_SHORT ).show ();
}
} );
数据(模型类)
public class Data {
private String gender;
private float weight;
private float height;
private String goals;
private String activity;
private int age;
private double bmi;
private int condition;
public Data(String gender, float weight, float height, String goals, String activity, int age, double bmi, int condition) {
this.gender = gender;
this.weight = weight;
this.height = height;
this.goals = goals;
this.activity = activity;
this.age = age;
this.bmi = bmi;
this.condition = condition;
}
public String getGender() {
return gender;
}
public float getWeight() {
return weight;
}
public float getHeight() {
return height;
}
public String getGoals() {
return goals;
}
public String getActivity() {
return activity;
}
UserStatsResponse
public class UserStatsResponse {
private boolean sucess;
private int status;
private Data data;
public UserStatsResponse(boolean sucess, int status, Data data) {
this.sucess = sucess;
this.status = status;
this.data = data;
}
public boolean isSucess() {
return sucess;
}
public int getStatus() {
return status;
}
public Data getData() {
return data;
}
}
发布请求
@POST("info/profile/")
Call<UserStatsResponse> userInfo(
@Header ( "Authorization" ) String token,
@Field ( "gender" ) String gender,
@Field("weight") int weight,
@Field("height") int height,
@Field ( "goals" ) String goals,
@Field ( "activity" ) String activity,
@Field ( "age" ) int age
);
JSON响应
{
"sucess": true,
"status": 200,
"data": {
"gender": "2",
"weight": 51.0,
"height": 148.0,
"goals": "4",
"activity": "2",
"age": 21,
"bmi": 23.283418553688826,
"condition": 2
}}
// logcat错误,在更改模型类之前和之后,问题仍然存在
2020-06-06 20:07:29.453 15964-15964/com.example.fitnessapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fitnessapp, PID: 15964
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Boolean com.example.fitnessapp.model_class.UserStatsResponse.getSucess()' on a null object reference
at com.example.fitnessapp.activities.UserStatistics$1.onResponse(UserStatistics.java:151)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
改造客户端
public class RetrofitClient {
private static final String BASE_URL = "https://kanishkarrevin.pythonanywhere.com/";
private static RetrofitClient mInstanceSignUp;
private Retrofit retrofitSignUp;
private RetrofitClient(){
retrofitSignUp = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory( GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance(){
if(mInstanceSignUp == null){
mInstanceSignUp = new RetrofitClient();
}
return mInstanceSignUp;
}
public Api getApi(){
return retrofitSignUp.create(Api.class);
}
}
答案 0 :(得分:0)
将模型类更改为以下类别:
public class Data {
@SerializedName("gender")
@Expose
private String gender;
@SerializedName("weight")
@Expose
private Double weight;
@SerializedName("height")
@Expose
private Double height;
@SerializedName("goals")
@Expose
private String goals;
@SerializedName("activity")
@Expose
private String activity;
@SerializedName("age")
@Expose
private Integer age;
@SerializedName("bmi")
@Expose
private Double bmi;
@SerializedName("condition")
@Expose
private Integer condition;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public String getGoals() {
return goals;
}
public void setGoals(String goals) {
this.goals = goals;
}
public String getActivity() {
return activity;
}
public void setActivity(String activity) {
this.activity = activity;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getBmi() {
return bmi;
}
public void setBmi(Double bmi) {
this.bmi = bmi;
}
public Integer getCondition() {
return condition;
}
public void setCondition(Integer condition) {
this.condition = condition;
}
}
UserStatsResponse.java
public class UserStatsResponse {
@SerializedName("sucess")
@Expose
private Boolean sucess;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("data")
@Expose
private Data data;
public Boolean getSucess() {
return sucess;
}
public void setSucess(Boolean sucess) {
this.sucess = sucess;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
答案 1 :(得分:0)
首先,使用JsonObject或String接收响应。
检查它能否具有正确的响应? 我想问题出在您的Java bean序列化中发生了什么。
您是否忘记配置您的改造支持gson convert?
例如:.addConverterFactory(GsonConverterFactory.create(getGson()))
答案 2 :(得分:0)
我认为问题就在这里。
if(userStatsResponse.isSuccess ()){
Log.d("error", String.valueOf ( userStatsResponse ) );
Intent intent = new Intent ( UserStatistics.this, DashboardActivity.class );
intent.setFlags ( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity ( intent );
}
在userStatsResponse类中,您具有isSucess()方法而不是isSuccess()。 尝试更换
isSuccess()
如果条件为
isSucess()