我在主题上看到了很多回应,但还不足以满足我的需求。
我希望能够从截击响应中返回一个类似于列表的响应,并将该列表传递给另一个要添加到我的数据库中的类。
我尝试使用一个接口和一个回调函数,但是响应仍然挂在另一个接口上。
我想要这样的东西
class Big:Interface{
override fun A():List<Model> = getResponseFromVolley()
fun getResponseFromVolley(){ setUp Volley request return List}
}
How can I do this
class RemoteDataSourceManager: ServiceCall {
override suspend fun getMovies(context: Context): Result<List<MovieEntity>> {
var results: Result<List<MovieEntity>>
getRemoteMovies(context, object : VolleyCallBack{
override fun onResponse(result: Result<List<MovieEntity>>): Result<List<MovieEntity>> {
results = result
return results
}
})
return results //result mustbe initialized
}
private fun getRemoteMovies(context: Context, callBack: VolleyCallBack): Result<List<MovieEntity>> {
val responseBool = MutableLiveData<Boolean>(false)
var listResult: Result<List<MovieEntity>>
val listOfMovies = ArrayList<MovieEntity>()
val url =
"https://api.themoviedb.org/3/movie/popular?api_key=f1e256985ebc2be710bf1f4ed754da11&language=en-US&page=1"
val request: JsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url, null,
Response.Listener { response ->
try {
val results = response.getJSONArray("results")
val size = results.length()
var title = ""
var movieImage = ""
var rating = 0
var overView = ""
var releaseDate = ""
var movieId = ""
for (movie in 0 until results.length()) {
val eachMovie = results.getJSONObject(movie)
movieImage = "https://image.tmdb.org/t/p/w500"
title = eachMovie.getString("title")
movieImage +=eachMovie.getString("poster_path")
rating = eachMovie.getInt("vote_average")
overView = eachMovie.getString("overview")
releaseDate = eachMovie.getString("release_date")
movieId = eachMovie.getInt("id").toString()
val newMovie = MovieEntity(
title,
movieImage,
rating,
overView,
releaseDate
)
newMovie.movieId = movieId
newMovie.id = movieId.toInt()
listOfMovies.add(newMovie)
responseBool.value = true
}
listResult = getResult(Result.Success(listOfMovies.toList()))
callBack.onResponse(listResult)
Log.i("insideList", "$listOfMovies")
} catch (e: Exception) {
Log.i("error", "Response $e")
// Toast.makeText(context!!.applicationContext, "No activity", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener { error ->
val errroMessage = VolleyErrorHandler.instance.erroHandler(error)
CoroutineScope(Dispatchers.Main).launch {
// Toast.makeText(context, errroMessage, Toast.LENGTH_LONG).show()
}
})
context.applicationContext.let {
VolleySingleton.getInstance(it).addToRequestQueue(request)
}
Log.i("listOfMovies", "$listResult")
return listResult
}
override suspend fun saveMovies(movieEntity: MovieEntity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
interface VolleyCallBack{
fun onResponse(result:Result<List<MovieEntity>>): Result<List<MovieEntity>>
}
private fun getResult(result: Result<List<MovieEntity>>):Result<List<MovieEntity>>{
return result
}
}
答案 0 :(得分:0)
interface VolleyCallBack{
fun onResponse(result: Result<List<MovieEntity>>)
}
在您的原始班级:
fun getMovies() {
// start request data from internet. Should have something to notify user that i'm requesting data (loading dialog for example)
//loadingdialog.show()
RemoteDataSorceManager().getRemoteMovies(context, object : VolleyCallBack{
override fun onResponse(result: Result<List<MovieEntity>>) {
// this is when you receive data
// loadingdialog.hide()
sendDataToOtherClassAndSaveToDatabase(result)
return@onResponse
}
})
}
我不熟悉Volley。只是我的意见。