为什么要在响应体中改造 returns null?
Why retrofit returns null in response body?
在响应正文中获取 null
响应结果。使用改造、匕首和存储库。为什么得到 null
作为回应我不知道。我的模型似乎是正确的。有什么问题吗?
MainViewModel.kt
@HiltViewModel
class MainViewModel@Inject constructor(
private val repository: MovieRepository,
@ApplicationContext private val context: Context
) : ViewModel() {
val movieList = MutableLiveData<Resource<Movie>>()
fun getAllMovies(movieName: String) {
movieList.postValue(Resource.Loading())
viewModelScope.launch {
try {
if (hasInternetConnection(context)) {
val response = repository.getMovies(movieName, "ffe9123f")
movieList.postValue(Resource.Success(response.body()!!))
} else
movieList.postValue(Resource.Error("Internet yok"))
} catch (ex: Exception) {
when (ex) {
is IOException -> movieList.postValue(Resource.Error("Network Failure " + ex.localizedMessage))
else -> movieList.postValue(Resource.Error("Conversion Error"))
}
}
}
}
}
Resource.kt
sealed class Resource<T>(
val data: T? = null,
val message: String? = null
) {
class Success<T>(data: T): Resource<T>(data)
class Error<T>(message: String, data: T? = null): Resource<T>(data, message)
class Loading<T> : Resource<T>()
}
MovieRepository.kt
@Singleton
class MovieRepository @Inject constructor(private val movieAppService: MovieAppService) {
suspend fun getMovies(title: String, aKey: String): Response<Movie> = withContext(
Dispatchers.IO
) {
val movies = movieAppService.getMovies(title = title, aKey = aKey)
movies
}
Movie.kt
data class Movie(
val title: String,
val year: String,
val rated: String,
val released: String,
val runtime: String,
val genre: String,
val director: String,
val writer: String,
val actors: String,
val plot: String,
val language: String,
val country: String,
val awards: String,
val poster: String,
val ratings: List<Rating>,
val metascore: String,
val imdbRating: String,
val imdbVotes: String,
val imdbID: String,
val type: String,
val dvd: String,
val boxOffice: String,
val production: String,
val website: String,
val response: String
)
MovieAppService.kt
interface MovieAppService {
companion object {
const val ENDPOINT = "http://www.omdbapi.com/"
}
@GET(".")
suspend fun getMovies(@Query("t") title: String,@Query("apikey") aKey: String): Response<Movie>
}
{"Title":"A Beautiful Mind","Year":"2001","Rated":"PG-13","Released":"04 Jan 2002","Runtime":"135 min","Genre":"Biography, Drama","Director":"Ron Howard","Writer":"Akiva Goldsman, Sylvia Nasar","Actors":"Russell Crowe, Ed Harris, Jennifer Connelly","Plot":"After John Nash, a brilliant but asocial mathematician, accepts secret work in cryptography, his life takes a turn for the nightmarish.","Language":"English","Country":"United States","Awards":"Won 4 Oscars. 37 wins & 69 nominations total","Poster":"https://m.media-amazon.com/images/M/MV5BMzcwYWFkYzktZjAzNC00OGY1LWI4YTgtNzc5MzVjMDVmNjY0XkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.2/10"},{"Source":"Rotten Tomatoes","Value":"74%"},{"Source":"Metacritic","Value":"72/100"}],"Metascore":"72","imdbRating":"8.2","imdbVotes":"908,920","imdbID":"tt0268978","Type":"movie","DVD":"25 Jun 2002","BoxOffice":"0,742,341","Production":"N/A","Website":"N/A","Response":"True"}
电影模型错误。没想到首字母是大写的
Movie.kt
data class Movie(
val Title: String,
val Year: String,
val Rated: String,
val Released: String,
val Runtime: String,
val Genre: String,
val Director: String,
val Writer: String,
val Actors: String,
val Plot: String,
val Language: String,
val Country: String,
val Awards: String,
val Poster: String,
val Ratings: List<Rating>,
val Metascore: String,
val imdbRating: String,
val imdbVotes: String,
val imdbID: String,
val Type: String,
val Dvd: String,
val boxOffice: String,
val Production: String,
val Website: String,
val Response: String
)
在响应正文中获取 null
响应结果。使用改造、匕首和存储库。为什么得到 null
作为回应我不知道。我的模型似乎是正确的。有什么问题吗?
MainViewModel.kt
@HiltViewModel
class MainViewModel@Inject constructor(
private val repository: MovieRepository,
@ApplicationContext private val context: Context
) : ViewModel() {
val movieList = MutableLiveData<Resource<Movie>>()
fun getAllMovies(movieName: String) {
movieList.postValue(Resource.Loading())
viewModelScope.launch {
try {
if (hasInternetConnection(context)) {
val response = repository.getMovies(movieName, "ffe9123f")
movieList.postValue(Resource.Success(response.body()!!))
} else
movieList.postValue(Resource.Error("Internet yok"))
} catch (ex: Exception) {
when (ex) {
is IOException -> movieList.postValue(Resource.Error("Network Failure " + ex.localizedMessage))
else -> movieList.postValue(Resource.Error("Conversion Error"))
}
}
}
}
}
Resource.kt
sealed class Resource<T>(
val data: T? = null,
val message: String? = null
) {
class Success<T>(data: T): Resource<T>(data)
class Error<T>(message: String, data: T? = null): Resource<T>(data, message)
class Loading<T> : Resource<T>()
}
MovieRepository.kt
@Singleton
class MovieRepository @Inject constructor(private val movieAppService: MovieAppService) {
suspend fun getMovies(title: String, aKey: String): Response<Movie> = withContext(
Dispatchers.IO
) {
val movies = movieAppService.getMovies(title = title, aKey = aKey)
movies
}
Movie.kt
data class Movie(
val title: String,
val year: String,
val rated: String,
val released: String,
val runtime: String,
val genre: String,
val director: String,
val writer: String,
val actors: String,
val plot: String,
val language: String,
val country: String,
val awards: String,
val poster: String,
val ratings: List<Rating>,
val metascore: String,
val imdbRating: String,
val imdbVotes: String,
val imdbID: String,
val type: String,
val dvd: String,
val boxOffice: String,
val production: String,
val website: String,
val response: String
)
MovieAppService.kt
interface MovieAppService {
companion object {
const val ENDPOINT = "http://www.omdbapi.com/"
}
@GET(".")
suspend fun getMovies(@Query("t") title: String,@Query("apikey") aKey: String): Response<Movie>
}
{"Title":"A Beautiful Mind","Year":"2001","Rated":"PG-13","Released":"04 Jan 2002","Runtime":"135 min","Genre":"Biography, Drama","Director":"Ron Howard","Writer":"Akiva Goldsman, Sylvia Nasar","Actors":"Russell Crowe, Ed Harris, Jennifer Connelly","Plot":"After John Nash, a brilliant but asocial mathematician, accepts secret work in cryptography, his life takes a turn for the nightmarish.","Language":"English","Country":"United States","Awards":"Won 4 Oscars. 37 wins & 69 nominations total","Poster":"https://m.media-amazon.com/images/M/MV5BMzcwYWFkYzktZjAzNC00OGY1LWI4YTgtNzc5MzVjMDVmNjY0XkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.2/10"},{"Source":"Rotten Tomatoes","Value":"74%"},{"Source":"Metacritic","Value":"72/100"}],"Metascore":"72","imdbRating":"8.2","imdbVotes":"908,920","imdbID":"tt0268978","Type":"movie","DVD":"25 Jun 2002","BoxOffice":"0,742,341","Production":"N/A","Website":"N/A","Response":"True"}
电影模型错误。没想到首字母是大写的
Movie.kt
data class Movie(
val Title: String,
val Year: String,
val Rated: String,
val Released: String,
val Runtime: String,
val Genre: String,
val Director: String,
val Writer: String,
val Actors: String,
val Plot: String,
val Language: String,
val Country: String,
val Awards: String,
val Poster: String,
val Ratings: List<Rating>,
val Metascore: String,
val imdbRating: String,
val imdbVotes: String,
val imdbID: String,
val Type: String,
val Dvd: String,
val boxOffice: String,
val Production: String,
val Website: String,
val Response: String
)