android 改装错误:应为字符串,但在第1行第92列路径$.belongs_to_collection处为开始_OBSTING

bttbmeg0  于 2023-09-29  发布在  Android
关注(0)|答案(1)|浏览(93)

我需要调用一个API来通过ID接收电影的详细信息。当我打第一个电话时,它工作正常。如果第二个呼叫与第一个呼叫具有相同的ID,则它也可以工作,如果我使用相同的ID进行呼叫,则依此类推。现在,如果我用一个ID进行第一次调用,用另一个ID进行第二次调用,应用程序崩溃并显示以下错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 92 path $.belongs_to_collection
我在这里寻找解决方案,但没有一个奏效。你能帮帮我吗?下面是我认为重要的代码。

Retrofit Interceptor

override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()
            .newBuilder()
            .addHeader("accept", "application/json")
            .addHeader("Authorization", Private.theMovieDatabaseHeaderAuthorization)
            .build()

        return chain.proceed(request)
    }
Retrofit Builder

private val client = OkHttpClient.Builder().apply {
        connectTimeout(60, TimeUnit.SECONDS)
        readTimeout(60, TimeUnit.SECONDS)
        addInterceptor(TheMovieDatabaseInterceptor())
    }.build()

    private val retrofit = Retrofit.Builder()
        .baseUrl(Private.theMovieDatabaseBaseUrl)
        .client(client)
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build()
Retrofit Endpoint

@GET("movie/{movie_id}?language=en-US")
suspend fun getMovieDetails(@Path("movie_id") movieId: String): MovieDetailsModel
MovieDetailsModel

data class MovieDetailsModel(
    @SerializedName("id")
    val id: Int,
    @SerializedName("adult")
    val adult: Boolean,
    @SerializedName("backdrop_path")
    val backdropPath: String,
    @SerializedName("belongs_to_collection")
    val belongsToCollection: String,
    @SerializedName("budget")
    val budget: Int,
    @SerializedName("genres")
    val genres: List<GenreModel>,
    @SerializedName("homepage")
    val homepage: String,
    @SerializedName("imdb_id")
    val imdbId: String,
    @SerializedName("original_language")
    val originalLanguage: String,
    @SerializedName("original_title")
    val originalTitle: String,
    @SerializedName("overview")
    val overview: String,
    @SerializedName("popularity")
    val popularity: Double,
    @SerializedName("poster_path")
    val posterPath: String,
    @SerializedName("production_companies")
    val productionCompanies: List<CompanyModel>,
    @SerializedName("production_countries")
    val productionCountries: List<CountryModel>,
    @SerializedName("release_date")
    val releaseDate: String,
    @SerializedName("revenue")
    val revenue: Int,
    @SerializedName("runtime")
    val runtime: Int,
    @SerializedName("spoken_languages")
    val spokenLanguages: List<LanguageModel>,
    @SerializedName("status")
    val status: String,
    @SerializedName("tagline")
    val tagline: String,
    @SerializedName("title")
    val title: String,
    @SerializedName("video")
    val video: Boolean,
    @SerializedName("vote_average")
    val voteAverage: Double,
    @SerializedName("vote_count")
    val voteCount: Int
)
Repository

override suspend fun getMovieDetails(movieId: String): MovieDetailsModel {
        return theMovieDatabaseEndpoints.getMovieDetails(movieId)
    }
ViewModel

private val _movieDetails: MutableLiveData<MovieDetailsModel> = MutableLiveData()
val movieDetails: LiveData<MovieDetailsModel> = _movieDetails

private fun getMovieDetails(movieId: String) = viewModelScope.launch {
    Log.d("getMovieDetails", "movieId: $movieId")
    _movieDetails.value = theMovieDatabaseApiUseCase.getMovieDetails(movieId)
}
7ivaypg9

7ivaypg91#

belongs_to_collection不返回String,正如API文档所述。
您可以查看更多here

相关问题