我正在尝试从 Kotlin 中的服务器获取一些数据,我希望对其进行一些进一步的处理,例如将其显示给用户。但执行并没有等待/阻塞结果返回,而是继续执行。
这是代码:
class UserLand : AppCompatActivity() {
class SyncViewModel(): ViewModel() {
suspend fun startingSync(accesstoken: String): String {
var response = ""
viewModelScope.launch(Dispatchers.IO) {
val reqParam = "token=$accesstoken"
val mURL = URL("<-- server end point here -->")
with(mURL.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "POST"
val wr = OutputStreamWriter(outputStream);
wr.write(reqParam);
wr.flush();
println("2 : $url")
println("3 : $responseCode")
BufferedReader(InputStreamReader(inputStream)).use {
var inputLine = it.readLine()
while (inputLine != null) {
response += inputLine
inputLine = it.readLine()
}
Log.d("4: ","Response : $response")
}
}
}
Log.d("5", response)
return response
}
}
fun syncWithServer(view: View) {
val accesstoken = "johndoe"
var response = ""
if (accesstoken != null) {
Log.d("----->", "1")
runBlocking {
response = SyncViewModel().startingSync(accesstoken)
Log.d("----->", "6")
}
}
//
Log.d("Final result: 7:---------> ", response)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_snyc_packages_with_server)
}
}
我通过单击屏幕上的按钮来调用 syncWithServer()
。此后,syncWithServer()
将在函数 startingSync()
中的单独线程上启动网络请求。现在我想要的日志的执行顺序如下:
1->2->3->4->5->6->7(根据日志和打印消息)。最后,在 log(7) 处,我将从服务器获取我想要进一步处理的响应。但实际执行结果如下:
1->6->2->3->4。请注意,5 和 7 未记录在 android logcats 中。我认为执行线程没有等待网络请求结果返回,因此没有实现理想的执行步骤。
我刚刚开始使用 kotlin 中的协同例程。我知道我错过了一些东西,但它到底是什么?
最佳答案
viewModelScope.launch(Dispatchers.IO)
在您的代码中创建并行工作,您有 2 个变体来修复它:
1 - 使用 withContenxt(Dispatchers.IO)
而不是 viewModelScope.launch(Dispatchers.IO)
,它只会更改调度程序
2 - 使用 viewModelScope.async(Dispatchers.IO)
而不是 viewModelScope.launch(Dispatchers.IO)
并通过调用 .await 等待结果()
该 block 的结尾
关于android - Kotlin 协程不等待网络结果返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64333182/