在Retrofit中将requestBody用于表单数据时,java.lang.ClassCastException

时间:2019-05-07 14:18:52

标签: android kotlin retrofit2

我有一个简单的任务用于API Rest调用,并进行了改进以登录。

class SignInTask constructor(
        private val apiService: ApiService,
        private val signIn: SignIn,
        private val db: Db
) : Runnable {
    private val _liveData = MutableLiveData<Resource<SignResponse>>()
    val liveData: LiveData<Resource<SignResponse>> = _liveData

    override fun run() {

            val newValue = try {
            val response = apiService.signIn(signIn).execute()
            val apiResponse = ApiResponse.create(response)

            when (apiResponse) {
                is ApiSuccessResponse -> {
                    _liveData.postValue(Resource.success(apiResponse.body))
                }
                is ApiEmptyResponse -> {
                    Resource.success(false)
                }
                is ApiErrorResponse -> {
                    Resource.error(apiResponse.errorMessage, true)
                }
            }
        } catch (e: IOException) {
            Resource.error(e.message!!, true)
        }
        _liveData.postValue(newValue as Resource<SignResponse>?)
    }
}

相应的改造API服务是:

@POST("/v1/signin/")
fun signIn(@Body signIn: SignIn): Call<SignResponse>

我必须将其更改为表单数据,因为后端已更改并请求它。所以我添加了requestBody

class SignInTask constructor(
        private val apiService: ApiService,
        private val signIn: SignIn,
        private val db: FastcountDb
) : Runnable {
    private val _liveData = MutableLiveData<Resource<SignResponse>>()
    val liveData: LiveData<Resource<SignResponse>> = _liveData

    override fun run() {

        val requestBody = MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("email", signIn.email)
                .addFormDataPart("password", signIn.password)
                .addFormDataPart("tenant", signIn.tenant.toString())
                .build()

        val newValue = try {

            val response = apiService.signIn(requestBody).execute()
            val apiResponse = ApiResponse.create(response)

            when (apiResponse) {
                is ApiSuccessResponse -> {
                    _liveData.postValue(Resource.success(apiResponse.body))
                }
                is ApiEmptyResponse -> {
                    Resource.success(false)
                }
                is ApiErrorResponse -> {
                    Resource.error(apiResponse.errorMessage, true)
                }
            }
        } catch (e: IOException) {
            Resource.error(e.message!!, true)
        }
        _liveData.postValue(newValue as Resource<SignResponse>?)
    }
}

retrofit API服务是:

@POST("/v1/signin/")
fun signIn(@Body body: RequestBody): Call<SignResponse>

唯一的更改是创建并添加了requestBody。 问题是现在应用程序崩溃了

_liveData.postValue(newValue as Resource<SignResponse>?)

使用

java.lang.ClassCastException: kotlin.Unit cannot be cast to at.ivii.fastcount.vo.Resource
        at at.ivii.fastcount.repository.task.SignInTask.run(SignInTask.kt:52)

在调试过程中,我看到API调用成功完成,并且响应正确,但是200崩溃了。

0 个答案:

没有答案