从错误响应获取数据时发生java.io.IOException

从错误响应获取数据时发生java.io.IOException,java,kotlin,fuel,Java,Kotlin,Fuel,我实现了一个简单的登录API请求 val fuelRequest = Fuel.post(urlString) .header("Content-Type", "application/json") .header("Accept", "application/json") .jsonBody(UtilsString.getStringForDictionary(params)) fuelR

我实现了一个简单的登录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)
}
当响应正常时,没有问题。当我试图从错误请求中获取数据响应时,就会出现问题。我得到的数据如下:

{
    "code": 401,
    "error": "The specified credentials are invalid."
}
这是一个401未经授权的响应。我只是想从de request那里得到消息。如果我尝试
response.data
它抛出方法抛出'java.io.IOException如果我尝试
result.component()2.response
它抛出方法抛出'android.os.networkonmainthreadeception'异常。无法计算com.github.kittinunf.fuel.core.Response.toString()如果我尝试
result.error.errorData
它会抛出方法抛出“java.io.IOException”


有什么线索可以让我得到回应吗?

你可以像这样得到回应,然后对回应做任何你想做的事情

Fuel.post("https://api.chui.ai/v1/enroll")
                            .header(headers)
                            .body(json.toString(), Charset.forName("UTF-8"))
                            .responseString(new com.github.kittinunf.fuel.core.Handler<String>() {

                            @Override
                            public void failure(@NotNull com.github.kittinunf.fuel.core.Request request,
                                                @NotNull com.github.kittinunf.fuel.core.Response response,
                                                @NotNull FuelError error) {
                                Log.d("Fuel.failure", error.getMessage());
                            }

                            @Override
                            public void success(@NotNull com.github.kittinunf.fuel.core.Request request,
                                                @NotNull com.github.kittinunf.fuel.core.Response response,
                                                String data) {
                                // data is a string, parse as using you fav json library
                                Log.d("Fuel.success", data);
                            }
Fuel.post(“https://api.chui.ai/v1/enroll")
.标题(标题)
.body(json.toString(),Charset.forName(“UTF-8”))
.responseString(新的com.github.kittinunf.fuel.core.Handler(){
@凌驾
公共无效失败(@NotNull com.github.kittinunf.fuel.core.Request,
@NotNull com.github.kittinunf.fuel.core.Response响应,
@NotNull(错误){
Log.d(“Fuel.failure”,error.getMessage());
}
@凌驾
public void success(@NotNull com.github.kittinunf.fuel.core.Request),
@NotNull com.github.kittinunf.fuel.core.Response响应,
(字符串数据){
//数据是一个字符串,使用fav json库进行解析
Log.d(“燃料成功”,数据);
}

我复制了您的代码,如果我添加此代码:

findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
            val fuelRequest = Fuel.post("http://10.0.2.2:3000/")
                    .header("Content-Type", "application/json")
                    .header("Accept", "application/json")
                    .jsonBody(UtilsString.getStringForDictionary(params))

            fuelRequest.response() { _, response, _ ->
                println(String(response.data))
            }
}

我无法在注释中添加代码段,但这是代码段。您可以接受类似于这样的请求,并根据您的用例执行任何您想要的操作

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) {
        ""
    }
}
导入com.github.kittinunf.fuel.fuel
导入com.github.kittinunf.fuel.core.FuelError
导入com.github.kittinunf.result.result
私有类响应者(
val状态代码:Int,
val状态消息:字符串,
val errorMessage:字符串
):RuntimeException(“[%d-%s]%s.”格式(statusCode、statusMessage、errorMessage))
趣味主线(args:Array){
val(请求、响应、结果)=燃料后(“http://httpbin.org/post)。负责人()
何时(结果){
is Result.Failure->onError(结果)
}
打印(“完成”)
}
/**
*
*/
错误(failureResult:Result.Failure){
投掷应答器(
statusCode=failureResult.error.response.statusCode,
statusMessage=failureResult.error.response.responseMessage,
errorMessage=failureResult.getErrorMessage())
}
/**
*
*/
private fun Result.Failure.getErrorMessage():字符串{
回击{
val string=string(this.error.errorData)
打印(字符串)
一串
}捕获(e:运行时异常){
""
}
}

请您将其更新到Kotlin好吗?它对我不起作用。我更新了我的答案,也许这对Hanks有帮助。让我检查一下
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) {
        ""
    }
}