레트로핏 클래스
private val retrofit: Retrofit = Retrofit.Builder() .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(serverIp) .build() val userApi: UserAPI = retrofit.create(UserAPI::class.java)
data 리퀘스트 클래스
data class LoginRequest( @SerializedName("userId") val userId: String?, @SerializedName("userName") val userName: String?, @SerializedName("password") val password: String?, @SerializedName("phone") val phone: String?, @SerializedName("gender") val gender: String?, @SerializedName("age") val age: Int?, @SerializedName("email") val email: String?, @SerializedName("provider") val provider: String?, @SerializedName("pushNotiFl") val pushNotiFl: String?, @SerializedName("kakaoNotiFl") val kakaoNotiFl: String?, @SerializedName("deviceToken") val deviceToken: String? )
유저 api
@POST("user") fun loginBody( @Body loginRequest : LoginRequest ) : Single<UserResponse>
유저 모델
fun loginBody(body: LoginRequest): Single<UserResponse>
유저 impl
override fun loginBody(body: LoginRequest): Single<UserResponse> { return RetrofitAPI.getInstance().userApi.loginBody(body) }
콜하는 뷰모델
fun loginEvent(userId: String?, userName: String?, password: String?, phone: String?, gender: String?, age: Int?, email: String?, provider: String?, pushNotiFl:String?, kakaoNotiFl: String?, deviceToken: String?) { val test: LoginRequest = LoginRequest(userId, userName, password, phone, gender, age, email, provider, pushNotiFl, kakaoNotiFl, deviceToken) Timber.d("Test Checked ! $test") addDisposable( model.loginBody(test) .subscribeOn(Schedulers.io()) .subscribe({ _loginResult.postValue(it) }, { Timber.d("response error, message : ${it.localizedMessage}") }) ) }
리스폰스 확인하는 액티비티
viewModel.loginResult.observe(this@LoginActivity, Observer { if (it.status == 200){ Timber.d("Test Checked) }else{ Timber.d("Test Checked error ${it.status} ${it.responseData.login.userId}") } })
okhttp log
포스트맨 결과
포스트맨이 정상인거 보면 서버쪽 에러는 아니고
activity 에서 response status가 0가 나오고있습니다
end point 에러라면 rxjava 사용한 viewmodel 에서 response error 이 나와야 정상이라고 생각합니다
하지만 액티비티에서 response status 가 나온다는건 서버에 정상적으로 api를 찌르는 데는 성공했으니 response 가 나온다고 생각합니다
그럼 여기서 response status가 0이 뜨고 다른 response data가 null 이뜨는 이유는 추측이지만 okhttp 로그를 확인해보니 json이 순서대로 만들어지지 않고 이상하게 조합이 되는 것 이라고 생각됩니다
여기서 json이 순서대로 조합되지 않는 이유도 잘모르겠습니다 리퀘스트 데이터에 시리얼라이즈 데이터도 재대로 맵핑해줬다고 생각되는데 이부분부터 고쳐나가야 다음 추측을 해보면서 풀어갈텐데 이부분이 이해가 안되서 혹시 아시는분 있으신가요? 아니면 솔루션을 제안해주셔도 감사할거같습니다