改造:来自 API 对 post 请求的空响应
Retrofit : Null response from the API on post request
在 Android 项目中使用改造集成 API 时,我在发出 post 请求时收到 API 的 null
响应因此我无法在共享首选项中保存任何数据,当我能够看到响应是 null
时,我无法在 logcat 中找到错误。
Activity
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 ();
}
} );
数据(型号class)
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请求
@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换模型前后报错classes,问题依旧
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.onResponse(UserStatistics.java:151)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.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);
}
}
将模型 类 更改为以下:
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;
}
}
首先,使用JsonObject或String接收响应。
检查它能有正确的反应吗?
我猜你的 java bean 序列化中发生了什么问题。
你忘了配置你的改造支持gson转换吗?
例如:.addConverterFactory(GsonConverterFactory.create(getGson()))
我认为问题出在这里。
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 class 你有 isSucess() 方法而不是 isSuccess()。
尝试替换
isSuccess()
在 if 条件
isSucess()
在 Android 项目中使用改造集成 API 时,我在发出 post 请求时收到 API 的 null
响应因此我无法在共享首选项中保存任何数据,当我能够看到响应是 null
时,我无法在 logcat 中找到错误。
Activity
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 ();
}
} );
数据(型号class)
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请求
@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换模型前后报错classes,问题依旧
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.onResponse(UserStatistics.java:151)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.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);
}
}
将模型 类 更改为以下:
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;
}
}
首先,使用JsonObject或String接收响应。
检查它能有正确的反应吗? 我猜你的 java bean 序列化中发生了什么问题。
你忘了配置你的改造支持gson转换吗?
例如:.addConverterFactory(GsonConverterFactory.create(getGson()))
我认为问题出在这里。
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 class 你有 isSucess() 方法而不是 isSuccess()。 尝试替换
isSuccess()
在 if 条件
isSucess()