[英]Waiting for firebase storage calls inside an HTTP request to continue
我有点卡在这里。 我已经将一些图像上传到我的 Firebase 存储,这些图像具有我通过 HTTP 发送到外部服务器的唯一 ID(带有标题等)。 现在我想在我的手机屏幕上看到这些图像,所以我必须使用 HTTP 来检索 uid,然后在我的存储中搜索每个图像:
package com.app.chotuve.home
import android.util.Log
import com.app.chotuve.context.ApplicationContext
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.extensions.jsonBody
import com.github.kittinunf.fuel.json.responseJson
import com.github.kittinunf.result.Result
import com.google.firebase.storage.FirebaseStorage
import org.json.JSONArray
class VideoDataSource {
companion object {
private val TAG: String = "Video Data Source"
private const val serverURL: String = "https://localhost:8081/videos"
fun createDataSet(): ArrayList<ModelVideo> {
val storage = FirebaseStorage.getInstance().reference
val list = ArrayList<ModelVideo>()
Fuel.get(serverURL)
.jsonBody("{ \"user\" : \"${ApplicationContext.getConnectedUsername()}\"," +
" \"token\" : \"${ApplicationContext.getConnectedUsername()}\"" +
"}") #Ignore this, it is to check if the connected user is valid.
.responseJson { request, response, result ->
when (result) {
is Result.Success -> {
val body = response.body()
val jsonList = JSONArray(body.asString("application/json"))
for (i in 0 until jsonList.length()) {
val item = jsonList.getJSONObject(i)
val date = item["date"] as String
val title = item["title"] as String
val user = item["user"] as String
val thumbURL: String = item["thumbnail"] as String
storage.child("thumbnails/").child(thumbURL).downloadUrl
.addOnSuccessListener {
var url = it.toString()
list.add(
ModelVideo(
title,
user,
url,
date
)
)
Log.d(TAG, "Success $url.")
}.addOnFailureListener {
Log.d(TAG, "Error obtaining Video: ${it.message}.")
}
}
}
is Result.Failure -> {
//Look up code and choose what to do.
Log.d(TAG, "Error obtaining Videos.")
}
}
}
return list
}
}
}
由于 Firebase 和 Fuel 都是异步的,因此数据列表总是返回空,我似乎找不到等待完整响应的方法。
免责声明:HTTP 是必需的。 我无法将该逻辑移至 firebase 数据库(尽可能多地)。
也许您可以像这样使用阻塞 HTTP 请求?
...
val list = ArrayList<ModelVideo>()
val (request, response, result) = serverURL.httpGet()
.jsonBody(
"{ \"user\" : \"${ApplicationContext.getConnectedUsername()}\"," +
" \"token\" : \"${ApplicationContext.getConnectedUsername()}\"" +
"}"
)
.responseJson()
when (result) {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.