Home > Software engineering >  Response Always null in Response body in Retrofit
Response Always null in Response body in Retrofit

Time:03-30

I'm trying to hit the api with retrofit its a POST request to registerd a user to app.Everything is fine but i didn't get the response inside the onResponse .The response code is 200 but still I'm not getting the response. I try to update my Model class many times but it can't workout.But in the postman I'm getting the response

Api interface

 interface Api {

    @POST("/v1/register")
    fun regsiterUser(@Body userRequest: UserRequest):Call<ResponseUpdate>

    @POST("v1/login")
    fun loginUser(@Body userRequest: UserRequest):Call<ModifiedLoginResponse>

    @POST("v1/listpost")
    fun getUPostList(@Body paramsUserList: ParamsUserList):Call<ResponseList>
} 

Activity Class

This functions is used to send the body in request

 fun fetch_data() : UserRequest {
          val userRequest: UserRequest =
              UserRequest()
          val confirmpassword = conPassword.text
          val email = email.text
          val password = password.text
          val fullname = fullname.text
        userRequest.setUser_name(fullname.toString())
        userRequest.setEmail_address(email.toString())
        userRequest.setPassword(password.toString())
        userRequest.setAndroid_token("a0lotYT8yHre6ljowFJNwRolXepYm3d4d4KUD_7353MSVFLJPUvLshpGAmM5TOjuAwaePAuolWTkV3g056NOlvidxw7nolF5UGqIK0")
        userRequest.setDevice_type(0)
        return userRequest
    }

This Fucntion is used to hit the Api

fun saveUser(userRequest: UserRequest){
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
             //  val okHttpClient = OkHttpClient().newBuilder().addInterceptor(httpLoggingInterceptor)
             val okHttpClient1 = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor)
             val retrofit = Retrofit.Builder()
                 .baseUrl("https://blaklif.com")
                 .addConverterFactory(GsonConverterFactory.create())
                 .client(okHttpClient1.build())
                 .build()
             val api = retrofit.create(Api::class.java)
             api.regsiterUser(userRequest).enqueue(object :Callback<ResponseUpdate>{
                 override fun onResponse(call: Call<ResponseUpdate>,response: Response<ResponseUpdate>,
                 ) { val responseUpdate = response.body()
                          val email = responseUpdate?.data?.email
                          if (response.isSuccessful){
                          Log.d("this is response ->", email.toString())
                      }
                 }
                 override fun onFailure(call: Call<ResponseUpdate>, t: Throwable, ) {
                 }
             })
         } 

Model Class

 public class ResponseUpdate{

    @SerializedName("data")
    private Data data;

    @SerializedName("success")
    private boolean success;

    @SerializedName("text")
    private String text;

    public Data getData(){
        return data;
    }

    public boolean isSuccess(){
        return success;
    }

    public String getText(){
        return text;
    }
} 

public class Data{

    @SerializedName("gender")
    private String gender;

    @SerializedName("login_type")
    private String loginType;

    @SerializedName("android_token")
    private String androidToken;

    @SerializedName("lif")
    private String lif;

    @SerializedName("bio")
    private String bio;

    @SerializedName("device_type")
    private String deviceType;

    @SerializedName("native_from")
    private String nativeFrom;

    @SerializedName("password")
    private String password;

    @SerializedName("u_id")
    private String uId;

    @SerializedName("ios_token")
    private Object iosToken;

    @SerializedName("google_plus_id")
    private String googlePlusId;

    @SerializedName("email")
    private String email;

    @SerializedName("website")
    private String website;

    @SerializedName("is_active")
    private String isActive;

    @SerializedName("live_in")
    private String liveIn;

    @SerializedName("qb_dialog_id")
    private String qbDialogId;

    @SerializedName("profile")
    private String profile;

    @SerializedName("token")
    private String token;

    @SerializedName("qb_id")
    private String qbId;

    @SerializedName("fb_id")
    private String fbId;

    @SerializedName("is_public")
    private String isPublic;

    @SerializedName("tagline")
    private String tagline;

    @SerializedName("location")
    private String location;

    @SerializedName("phone_number")
    private String phoneNumber;

    @SerializedName("username")
    private String username;

    public String getGender(){
        return gender;
    }

    public String getLoginType(){
        return loginType;
    }

    public String getAndroidToken(){
        return androidToken;
    }

    public String getLif(){
        return lif;
    }

    public String getBio(){
        return bio;
    }

    public String getDeviceType(){
        return deviceType;
    }

    public String getNativeFrom(){
        return nativeFrom;
    }

    public String getPassword(){
        return password;
    }

    public String getUId(){
        return uId;
    }

    public Object getIosToken(){
        return iosToken;
    }

    public String getGooglePlusId(){
        return googlePlusId;
    }

    public String getEmail(){
        return email;
    }

    public String getWebsite(){
        return website;
    }

    public String getIsActive(){
        return isActive;
    }

    public String getLiveIn(){
        return liveIn;
    }

    public String getQbDialogId(){
        return qbDialogId;
    }

    public String getProfile(){
        return profile;
    }

    public String getToken(){
        return token;
    }

    public String getQbId(){
        return qbId;
    }

    public String getFbId(){
        return fbId;
    }

    public String getIsPublic(){
        return isPublic;
    }

    public String getTagline(){
        return tagline;
    }

    public String getLocation(){
        return location;
    }

    public String getPhoneNumber(){
        return phoneNumber;
    }

    public String getUsername(){
        return username;
    }
} 

Json Response

{
    "success": true,
    "data": {
        "u_id": "324",
        "username": "shivam",
        "profile": "",
        "email": "[email protected]",
        "location": "",
        "website": "",
        "lif": "",
        "phone_number": "",
        "gender": "",
        "bio": "",
        "tagline": "",
        "live_in": "",
        "native_from": "",
        "password": "d386cef906fde4ccff27cfcdc8385ba4",
        "token": "fQkNVUsP3mlBdJuoGe1T6iAD7",
        "device_type": "0",
        "android_token": "APA91bEAfoucYYgXO_t1ENfJvY2hRXdH5SUFjFDAZL2kgjes_jYyo15tPPI0fYHkLTmuagQbHAth5dmA6uPdpB_dhJhcwt47t9vR2ChldINDmJ5vGmcWcOyUg5Uqc0cR0lflqD3oaoRs",
        "ios_token": null,
        "login_type": "user",
        "fb_id": "",
        "google_plus_id": "",
        "qb_id": "",
        "qb_dialog_id": "",
        "is_public": "1",
        "is_active": "1"
    },
    "text": "Your account register successfully."
}

I updated My model class May times but i can't find why I'm not getting the response

CodePudding user response:

you need to send your argument in form-data. Currently you are passing your data as an object. Refer this link for send form-data using retrofit2.

CodePudding user response:

try like this

@POST("/v1/register")
@FormUrlEncoded
fun regsiterUser(
    @Field("user_name") userName: String
    // Your all fields as per postman call
): Call<ResponseUpdate>
  • Related