android - 为什么我的改造模型返回 null?安卓科特林
问题描述
所以我正在尝试使用 themoviedb 来提取电影的搜索结果。网址如下:
https://api.themoviedb.org/3/search/movie?api_key={apikey}&language=en-US&query={query}
我在查询中插入要搜索的关键字的位置。我正在使用改造库来做到这一点。
这是我的 ApiService 代码:
interface ApiService {
@GET("3/search/movie?api_key=${BuildConfig.MOVIE_TOKEN}&language=en-US&")
fun getMovies(
@Query("query") query: String
): Call<SearchMovieResponse>
}
这是我的 ApiConfig 对象代码:
class ApiConfig {
companion object {
fun getApiService(): ApiService{
val loggingInterceptor =
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://api.themoviedb.org/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
return retrofit.create(ApiService::class.java)
}
}
}
我还有一个RemoteDataSouce
类使用该配置来获取电影。我还生成了使用 POJO 的数据类。这是RemoteDataSource
使用该 API 配置的类中的方法。
fun getMovies():List<MoviesItem>?{
val client = ApiConfig.getApiService().getMovies("john")
var listMovies: ArrayList<MoviesItem> = ArrayList<MoviesItem>()
client.enqueue(object: Callback<SearchMovieResponse> {
override fun onResponse(call: Call<SearchMovieResponse>, response: Response<SearchMovieResponse>) {
if (response.isSuccessful){
val rawList = response.body()?.results!!
for (item in rawList){
listMovies.add(item)
}
}
}
override fun onFailure(call: Call<SearchMovieResponse>, t: Throwable) {
return
}
})
return listMovies
}
我使用的数据模型SearchMovieResponse
是这样的:
data class SearchShowResponse(
@field:SerializedName("page")
val page: Int? = null,
@field:SerializedName("total_pages")
val totalPages: Int? = null,
@field:SerializedName("results")
val results: List<ShowsItem?>? = null,
@field:SerializedName("total_results")
val totalResults: Int? = null
)
data class ShowsItem(
@field:SerializedName("first_air_date")
val firstAirDate: String? = null,
@field:SerializedName("overview")
val overview: String? = null,
@field:SerializedName("original_language")
val originalLanguage: String? = null,
@field:SerializedName("genre_ids")
val genreIds: List<Int?>? = null,
@field:SerializedName("poster_path")
val posterPath: String? = null,
@field:SerializedName("origin_country")
val originCountry: List<String?>? = null,
@field:SerializedName("backdrop_path")
val backdropPath: String? = null,
@field:SerializedName("original_name")
val originalName: String? = null,
@field:SerializedName("popularity")
val popularity: Double? = null,
@field:SerializedName("vote_average")
val voteAverage: Double? = null,
@field:SerializedName("name")
val name: String? = null,
@field:SerializedName("id")
val id: Int? = null,
@field:SerializedName("vote_count")
val voteCount: Int? = null
)
但是,listMovies 返回 null。我不确定我在这里做错了什么。谁能解释一下?谢谢
解决方案
您的方法getMovies()
在 Retrofit 调用完成之前返回列表,您正在使用enqueue()
异步运行它的方法,因此您的方法在onResponse()
调用该方法之前完成。
解决方案,重写你的代码考虑这些信息或改用execute()
方法enqueue()
,这将在主线程中执行调用,所以你必须在新线程或协程中调用它。
推荐阅读
- reactjs - React-Apollo Compose - 等待所有链接的 graphql 查询完成
- javascript - 使用纯 javascript 获取不包含图形的元素
- php - 在 Genesis 面包屑中执行 WordPress 简码
- bash - 如何计算字符串bash shell中是否有3个或更多字符的字符串
- javascript - 删除连续的重复字符
- c++ - lu_factorize 损坏数据(行列式)
- sql - select * from ... 是什么意思? where 1=condition in sql
- c# - 无法分配给“ ”,因为它是“方法组”初学者
- android - Delphi Android TEdit 唯一编号
- ruby-on-rails - FactoryBot 事件 ID