Solution 1 :
Change the Model classes to below:
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;
}
}
Solution 2 :
First,Using JsonObject or String receive the response.
Check it can has correct response?
I guess the problem what happend in your java bean serialize.
Do you forget config your retrofit support gson convert?
eg:.addConverterFactory(GsonConverterFactory.create(getGson()))
Solution 3 :
I think problem is here.
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 );
}
In userStatsResponse class u have isSucess() method not isSuccess().
try replacing
isSuccess()
in if condition with
isSucess()
Problem :
While integrating an API using retrofit in an Android project, I am getting a null
response from the API when making a post request therefore I am not able to save any data in the shared preferences, I am not able to find the error in logcat when i am able to see that the response is null
.
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 ();
}
} );
Data (model 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 Request
@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 Response
{
"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 error before and after changing the model classes, the problem still persists
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)
Retrofit Client
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);
}
}
Comments
Comment posted by Sairaj Sawant
Can you check if any of the parameters for
Comment posted by kanishka
Yes i am getting a response if i am passing the same parameters in Postman
Comment posted by Sairaj Sawant
Are these parameter values not null? Are you able to see them in logcat?
Comment posted by kanishka
I am able to see all the parameters value in postman
Comment posted by Sairaj Sawant
Add a log.d in the code before
Comment posted by kanishka
The problem still persists
Comment posted by kanishka
Please check the logcat error, that is the NullPointerException
Comment posted by kanishka
I configured it, i have added the code for it as well
Comment posted by Longalei
you can put the retrofit log about http request and response for more infomation .
Comment posted by Sairaj Sawant
That would be a compile time issue. Not a NullPointer
Comment posted by Akash Bisht
You can a debug point on if statement. and check userStatsResponse. and check it’s members value
Comment posted by kanishka
Yes exactly, but why the null pointer exception is occuring in the first place
Comment posted by kanishka
yes i added it just now, the retrofit client seems to be okay, because the other api calls are behaving perfectly normal
Comment posted by Sairaj Sawant
Is the bearer token correct? Mostly you are missing on authentication as the response is 200 but it cannot be converted to UserStatsResponse. Can you check that once?