Retrofit 中的 Response body 中的 Response Always null

Response Always null in Response body in Retrofit

我正在尝试通过改造 POST 请求将用户注册到 app.Everything 来打 api 很好,但我没有得到onResponse 中的响应。响应代码是 200,但我仍然没有收到响应。我尝试更新我的 模型 class 很多次,但它不能 workout.But 在邮递员中 我收到了回复

Api接口

 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, ) {
                 }
             })
         } 

型号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响应

{
    "success": true,
    "data": {
        "u_id": "324",
        "username": "shivam",
        "profile": "",
        "email": "intelhybrid31230@gmail.com",
        "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."
}

我更新了我的模型 class 五次,但我找不到为什么我没有收到回复

您需要在 form-data 中发送您的论点。目前您正在将数据作为对象传递。使用 retrofit2 将此 link 用于

这样试试

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