显示在Retrofit OnFailure 情况下收到的响应消息

问题描述 投票:0回答:1

当来自 api 调用的数据 isSuccess 为 false 时,我尝试打印响应消息。但是,作为响应,我得到 isSuccess= false 但当我尝试打印消息时,它说数据为空。请帮我解决这个问题。

这是我调用 api 并观察 partsDetailData 的类。它进入 else 循环而不是进入 false 循环,尽管我得到的 isSuccess 为 false。邮递员结果显示 200。

期望:显示“部件 - 5272018 被取代为新部件号 - 6164702”作为 Toast,在 isSuccess=false 的情况下调用 partsDetailApi 时收到响应。

调试时,这里数据为空。

产品价格可用性.kt

val partsDetailsData = MutableLiveData<Resource<BaseResponse<PartsDetailsResponse>>>()    

productDetailEtaVM.partsDetailsData.observe(viewLifecycleOwner, Observer {
            it.let {
                dismissProgressBar()
                when(it.data?.isSuccess){
                    true ->{
                        //some code
                    }
                    false ->{
                    //**I want to enter in this condition**
                        putToast(it.data.message)

                    }
                    else ->{
                    //**My code enters in this condition** 
                        putToast("Sorry! You are not authorized to Order this Part.")

                    }
                }
            }
       })

产品详细信息EtaViewModel.kt

fun partsDetailsApi(){
        mShowProgressBar.value = true
        LoginControllerRepository.getInstance().partsDetailsApi(partsDetailsData,Constants.materialNo,Constants.sourceSupply,
                currentDateApi(),Constants.quantity!!,sharedPreference?.getString(Constants.customerCode)!!,sharedPreference?.getString(Constants.customerCode)!!,sharedPreference?.getString(Constants.customerCode)!!,"00","10")
    }

存储库类.kt

fun partsDetailsApi(responseData: MutableLiveData<Resource<BaseResponse<PartsDetailsResponse>>>, materialNo: String, sourceSupply: String,
                        pricingValidon : String, quantify : String, shipTo : String, soldTo : String, billTo : String, division : String, distChannel : String){
        apiService.loginUser()?.partsDetailsApi(materialNo = materialNo,sourceSupply = sourceSupply,pricingValidon = pricingValidon,
                quantity = quantify,shipTo = shipTo,soldTo = soldTo,billTo = billTo,division = division,distChannel = distChannel)
                ?.enqueue(getCallback(responseData))
    }

接口类.kt

@GET("Parts/PartsDetails?")
    fun partsDetailsApi(
            @Query("MaterialNo") materialNo : String,
            @Query("Sourcesupply") sourceSupply : String,
            @Query("PricingValidon") pricingValidon : String,
            @Query("Quantity") quantity : String,
            @Query("ShipTo") shipTo : String,
            @Query("SoldTo") soldTo : String,
            @Query("BillTo") billTo : String,
            @Query("Division") division : String,
            @Query("DistChannel") distChannel : String
    ) : Call<BaseResponse<PartsDetailsResponse>>

回应

{"isSuccess":false,"message":"Part - 5272018 is superseded to new Part number - 6164702","response":"Error"}

资源.kt

data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
    companion object {
        fun <T> success(data: T): Resource<T> = Resource(status = Status.SUCCESS, data = data, message = null)

        fun <T> error(message: String,data: T?): Resource<T> =
            Resource(status = Status.ERROR, data = data, message = message)

        fun <T> loading(data: T?): Resource<T> = Resource(status = Status.LOADING, data = data, message = null)
    }
}

BaseRepository.kt

调试完成后,我的编译器进入 OnFailure 状态。

package com.app.gmmcoassist.data.repository

import androidx.lifecycle.MutableLiveData
import com.app.gmmcoassist.utils.Resource
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.IOException

open class BaseRepository {
    private val noNetworkMsg = "No internet connection or network failure"

    fun <T> getCallback(responseData: MutableLiveData<Resource<BaseResponse<T>>>): Callback<BaseResponse<T>> {
        return object : Callback<BaseResponse<T>> {
            override fun onFailure(call: Call<BaseResponse<T>>, t: Throwable) {
                val msg = if (t is IOException) noNetworkMsg else t.message!!
                responseData.value = Resource.error(msg, null)
                System.out.println("getCallBackaaa"+"response.code()")
            }

            override fun onResponse(call: Call<BaseResponse<T>>, response: Response<BaseResponse<T>>) {
                if (response.isSuccessful && response.body() != null) {
                    responseData.value = Resource.success(response.body()!!)
                    System.out.println("getCallBack::::"+response.code())
                } else if (response.errorBody() != null) {
                    //val errorResponse = getErrorResponse<T>(response.errorBody()!!)
                    System.out.println("getCallBack"+response.code())
                     responseData.value = Resource.error(response.code().toString(), null)
                    if (response.code() == 204){
                        responseData.value = Resource.error(response.code().toString(), null)
                    }
                }

            }

        }
    }

    fun <T> getCallbackList(responseData: MutableLiveData<Resource<BaseResponse<List<T>>>>): Callback<BaseResponse<List<T>>> {
        return object : Callback<BaseResponse<List<T>>> {
            override fun onFailure(call: Call<BaseResponse<List<T>>>, t: Throwable) {
                val msg = if (t is IOException) noNetworkMsg else t.message!!
                responseData.value = Resource.error(msg, null)
            }


        }
    }

}
android kotlin retrofit2 response okhttp
1个回答
0
投票

好的各位,我已经解决了这个问题。我收到的响应是一个字符串,因此它给了我 IllegalStateException,json 未成功解析。

{"isSuccess":false,"message":"部件 - 5272018 被取代为新部件号 - 6164702","response":"Error"}

© www.soinside.com 2019 - 2024. All rights reserved.