Search code examples
javakotlinfuel

java.io.IOException when getting data from error response


I have implemented a simple POST request for login API.

val fuelRequest = Fuel.post(urlString)
    .header("Content-Type", "application/json")
    .header("Accept", "application/json")
    .jsonBody(UtilsString.getStringForDictionary(params))

fuelRequest.response() { _, response, result ->
    ...
    callback.onComplete(result)
}

When the response is ok, there is no problem. The problem occurs when I try to get the data response from an error request. The data that I get is this :

{
    "code": 401,
    "error": "The specified credentials are invalid."
}

And this comes with a 401 Unauthorised response. I've just wanted to get the message from de request. If I try response.data it throws Method threw 'java.io.IOException If I try result.component()2.response it throws Method threw 'android.os.NetworkOnMainThreadException' exception. Cannot evaluate com.github.kittinunf.fuel.core.Response.toString() If I try result.error.errorData it throws Method threw 'java.io.IOException

Any clue how can I get the response?


Solution

  • I couldn't add code snippet in the comment but here is the code snippet. You could take the request something like this and do whatever you want according to your usecase.

    import com.github.kittinunf.fuel.Fuel
    import com.github.kittinunf.fuel.core.FuelError
    import com.github.kittinunf.result.Result
    
    private class ResponseError(
            val statusCode: Int,
            val statusMessage: String,
            val errorMessage: String
    ) : RuntimeException("[%d - %s] %s".format(statusCode, statusMessage, errorMessage))
    
    fun main(args: Array<String>) {
    
        val (request, response, result) = Fuel.post("http://httpbin.org/post").responseString()
    
        when (result) {
            is Result.Failure -> onError(result)
        }
        print("done")
    }
    
    /**
     *
     */
    fun onError(failureResult: Result.Failure<String, FuelError>) {
        throw ResponseError(
                statusCode = failureResult.error.response.statusCode,
                statusMessage = failureResult.error.response.responseMessage,
                errorMessage = failureResult.getErrorMessage())
    }
    
    /**
     *
     */
    private fun Result.Failure<String, FuelError>.getErrorMessage(): String {
        return try {
            val string = String(this.error.errorData)
            print(string)
            string
        } catch (e: RuntimeException) {
            ""
        }
    }