等待HTTP请求内的Firebase存储调用以继续
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了等待HTTP请求内的Firebase存储调用以继续相关的知识,希望对你有一定的参考价值。
我有点困在这里。我已将一些具有唯一ID的图像上传到我的Firebase存储中,这些ID已通过HTTP(带有标题等)发送到了外部服务器。现在,我想在手机屏幕上看到这些图像,因此我必须使用HTTP来检索uid的ID,然后搜索存储中的每个图像:
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)
...
以上是关于等待HTTP请求内的Firebase存储调用以继续的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Cloud Functions for Firebase 中使 HTTP 请求异步/等待?