Friday, 11 March 2022

How to create empty Response.error body and send to UI in OkHttp kotlin? [Not solved]

I'm getting this

java.lang.IllegalArgumentException: code < 400: -1

Code:

suspend fun getReviewData() = getResult {
        try {
            apiService.getReviewData(getCustomerId())
        } catch (e: Exception) {
            val response: Response<ReviewResponse> = Response.error(-1, addBody())
            response
        }
    }

    private fun addBody(response: okhttp3.Response? = null): ResponseBody {
        val jsonObject = JSONObject()
        val contentType: MediaType? = response?.body?.contentType()
        return jsonObject.toString().toResponseBody(contentType)
    }

I feel that there is something wrong in addBody()

Exception is coming in this line and app is crashing:

val response: Response<ReviewResponse> = Response.error(-1, addBody())

Activity:

    viewModel.getReviewData().observe(this, new Observer<Resource<ReviewResponse>>() {
                @Override
                public void onChanged(Resource<ReviewResponse> reviewResponseResource) 
{ ....


from How to create empty Response.error body and send to UI in OkHttp kotlin? [Not solved]

No comments:

Post a Comment