【发布时间】:2020-09-25 14:49:24
【问题描述】:
在 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;
}
用户统计响应
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);
}
}
【问题讨论】:
-
您能否检查
userInfoAPI 的任何参数是否为空?您是否通过使用 Postman 访问 API 来获得响应? -
是的,如果我在 Postman 中传递相同的参数,我会收到响应
-
这些参数值不为空吗?你能在 logcat 中看到它们吗?
-
我可以看到邮递员中的所有参数值
-
在
call = RetrofitClient..之前的代码中添加log.d,打印性别、token等参数并检查是否不为空?