I'm kinda stuck here. I've uploaded some images to my Firebase Storage with unique IDs which I've sent to an external server via HTTP (with a title and such). Now I want to see those images in my phone screen so I have to use HTTP to retrieve the uid's and then search for each image in my storage:
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
}
}
}
Since both Firebase and Fuel are async, the data list is always returned empty and I can't seem to find a way of waiting for the full response.
DISCLAIMER: HTTP is required. I can't move that logic to firebase database (as much as I would want to).
Maybe you can just use blocking HTTP request like this?
...
val list = ArrayList<ModelVideo>()
val (request, response, result) = serverURL.httpGet()
.jsonBody(
"{ \"user\" : \"${ApplicationContext.getConnectedUsername()}\"," +
" \"token\" : \"${ApplicationContext.getConnectedUsername()}\"" +
"}"
)
.responseJson()
when (result) {
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.