无法为改造 2.Response<...> 创建调用适配器
我的 API:
@GET("/cinema/notShownMovies")
fun getNotShownMovies(
@Query("token") token: String
): Response<GetMovieResponse>
@JsonClass(generateAdapter = true)
data class Movie(
val id: Long,
val name: String,
val date: String,
@field:Json(name = "trailer_link")
val trailerLink: String?,
@field:Json(name = "poster_link")
val posterLink: String?,
@field:Json(name = "booked_tickets")
val bookedTickets: Int,
@field:Json(name = "movie_year_id")
val movieYearId: Int,
@field:Json(name = "created_at")
val createdAt: String,
@field:Json(name = "updated_at")
val updatedAt: String,
@field:Json(name = "worker_id")
val workerId: Int?,
@field:Json(name = "worker_name")
val workerName: String?,
@field:Json(name = "emergency_worker_id")
val emergencyWorkerId: Int?,
@field:Json(name = "emergency_worker_name")
val emergencyWorkerName: String?,
@field:Json(name = "booked_tickets_for_yourself")
val bookedTicketsForYourself: Int,
@field:Json(name = "view_movie")
val viewMovie: ViewMovie
)
@JsonClass(generateAdapter = true)
data class ViewMovie(
val href: String,
val method: String
)
尝试调用 API 时出现异常:
java.lang.IllegalArgumentException:无法创建调用适配器retrofit2.Response<...data.getmovieresponse>对于方法 InstanceApi.getNotShownMovies 无法为 retrofit2.Response<...data.getmovieresponse> 创建调用适配器对于方法 InstanceApi.getNotShownMovies
java.lang.IllegalArgumentException: Unable to create call adapter for retrofit2.Response<...data.GetMovieResponse> for method InstanceApi.getNotShownMovies Unable to create call adapter for retrofit2.Response<...data.GetMovieResponse> for method InstanceApi.getNotShownMovies
我不知道从哪里开始.所有其他 API 调用都可以正常工作,这些调用也在同一个 API 类中定义.也许是模型错误?
I don't know where to start. All other API calls work fine which is also defined in the same API class. Maybe a model error?
如果使用 coroutines
只需添加 suspend
修饰符.这将解决问题.
Just add suspend
modifier if using coroutines
. This will solve the problem.
否则,您的问题很可能是因为在实例化您的 Retrofit
对象时没有添加 Call 适配器.例如,对于 RxJava2
,您可以通过在构建时添加此行来包含调用适配器.
Otherwise your issue is likely because there is no Call adapter added when instantiating your Retrofit
object.
For example, for RxJava2
, you can include a call adapter by adding this line while building it.
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))