Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Fuel httpGet()responseString,失败:com.github.kittinunf.Fuel.core.BubbleFuelError:null 问题_Android_Kotlin_Kotlin Fuel - Fatal编程技术网

Android Fuel httpGet()responseString,失败:com.github.kittinunf.Fuel.core.BubbleFuelError:null 问题

Android Fuel httpGet()responseString,失败:com.github.kittinunf.Fuel.core.BubbleFuelError:null 问题,android,kotlin,kotlin-fuel,Android,Kotlin,Kotlin Fuel,我希望通过使用Fuel作为Httpclient对API进行同步调用来获得get请求的结果。 我在安卓(Anko)项目中使用燃料。 该调用只是一个简单的get请求,它总是失败,并出现以下错误: 失败:com.github.kittinunf.fuel.core.BubbleFuelError:null 原因:com.github.kittinunf.fuel.core.BubbleFuelError:null 背景 我想创建一个函数,用于返回使用Fuel的简单get请求的结果。但我无法检索同步结果

我希望通过使用Fuel作为Httpclient对API进行同步调用来获得get请求的结果。 我在安卓(Anko)项目中使用燃料。 该调用只是一个简单的get请求,它总是失败,并出现以下错误:

失败:com.github.kittinunf.fuel.core.BubbleFuelError:null

原因:com.github.kittinunf.fuel.core.BubbleFuelError:null

背景 我想创建一个函数,用于返回使用Fuel的简单get请求的结果。但我无法检索同步结果

我在网上找不到任何关于这个问题的有用信息

我试图通过使用协程和awaitStringResponse函数来等待结果。-->没有按预期工作

刚刚回答了一个关于这个主题的Github问题(标记为bug)。

有解决办法吗

代码示例 此代码正在运行:

但是使用这个函数,我无法返回结果

此代码不起作用


我找到了一种方法来解决这个问题

fun doRequest() = runBlocking {
    val (_, _, result) = Fuel.get("https://jsonplaceholder.typicode.com/posts/1").awaitStringResponse()
    result
}
使用runBlocking将阻塞当前线程,直到它完成

资料来源:

如果不想阻止当前线程,可以在新线程中启动此函数,如下所示:

Thread(
    Runnable {
      val result = doRequest()
      Log.e("Result", result)
    }
).start()
如果有人知道更好的处理方法,请展示您的解决方案

fun doRequest() = runBlocking {
    val (_, _, result) = Fuel.get("https://jsonplaceholder.typicode.com/posts/1").awaitStringResponse()
    result
}
Thread(
    Runnable {
      val result = doRequest()
      Log.e("Result", result)
    }
).start()