HTTP METHODS | Collection resource, such as https://api.example.com/collection/ | Member resource, such as https://api.example.com/collection/item3 |
---|---|---|
GET | Retrieve the URIs of the member resources of the collection resource in the response body. | Retrieve representation of the member resource in the response body. |
POST | Create a member resource in the collection resource using the instructions in the request body. The URI of the created member resource is automatically assigned and returned in the response Location header field. | Create a member resource in the member resource using the instructions in the request body. The URI of the created member resource is automatically assigned and returned in the response Location header field. |
PUT | Replace all the representations of the member resources of the collection resource with the representation in the request body, or create the collection resource if it does not exist. | Replace all the representations of the member resource or create the member resource if it does not exist, with the representation in the request body. |
PATCH | Update all the representations of the member resources of the collection resource using the instructions in the request body, or may create the collection resource if it does not exist. | Update all the representations of the member resource, or may create the member resource if it does not exist, using the instructions in the request body. |
DELETE | Delete all the representations of the member resources of the collection resource. | Delete all the representations of the member resource. |
Source: https://en.wikipedia.org/wiki/Representational_state_transfer (11/2019)
For examples visit https://restfulapi.net/json-data-types/
{
"Search":[
{
"Title":"I, Robot",
"Year":"2004",
"imdbID":"tt0343818",
"Type":"movie",
"Poster":"https://m.media-amazon.com/images/M/MV5BNmE1OWI2ZGItMDUyOS00MmU5LWE0MzUtYTQ0YzA1YTE5MGYxXkEyXkFqcGdeQXVyMDM5ODIyNw@@._V1_SX300.jpg"
},
{
"Title":"Robot Revolution: The Making of 'I, Robot'",
"Year":"2004",
"imdbID":"tt0427788",
"Type":"movie",
"Poster":"N/A"
},
{
"Title":"Day Out of Days: The 'I, Robot' Production Diaries",
"Year":"2004",
"imdbID":"tt0457853",
"Type":"movie",
"Poster":"N/A"
},
],
"totalResults":"3",
"Response":"True"
}
Link: https://square.github.io/retrofit/
Link: https://github.com/google/gson
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes; something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.
Source: https://github.com/google/gson (11/2019)
Add retrofit and gson converter dependency to Gradle
implementation(libs.retrofit)
JSON:
{
"Title": "Ghost in the Shell",
"Year": "1995",
"imdbID": "tt0113568",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BYWRiYjQyOGItNzQ1Mi00MGI1LWE3NjItNTg1ZDQwNjUwNDM2XkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg"
}
Kotlin Class:
data class MovieDTO (
val imdbID: String,
val Title: String,
val Year: String,
val Poster: String
)
JSON:
{
"Search": [
{
"Title": "Ghost in the Shell",
"Year": "2017",
"imdbID": "tt1219827",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BMzJiNTI3MjItMGJiMy00YzA1LTg2MTItZmE1ZmRhOWQ0NGY1XkEyXkFqcGdeQXVyOTk4MTM0NQ@@._V1_SX300.jpg"
},
{
"Title": "Ghost in the Shell",
"Year": "1995",
"imdbID": "tt0113568",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BYWRiYjQyOGItNzQ1Mi00MGI1LWE3NjItNTg1ZDQwNjUwNDM2XkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg"
}
]
}
Java Class:
class SearchResponse (
@SerializedName("Search") var search: List<Movie>
)
interface MovieApi {
@GET(".")
suspend fun getSearchResult(
@Query("s") movieSearch: String,
@Query("apikey") apiKey: String = apikey
): SearchResponse
}
val data = ArrayList<MovieDTO>()
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder().baseUrl("https://www.omdbapi.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val movieApi = retrofit.create(MovieApi::class.java)
val searchString = "Ghost in the Shell"
lifecycleScope.launch(Dispatchers.IO) {
try {
val body = movieApi.getSearchResult(searchString, "MySecretApiKey")
data.addAll(body.search.map { return@map it.toDomain() })
} catch ( e : Exception ) {
Log.e("Oh noo.", e.message)
}
}
We use the public free API from OMDb API for this example. You need to put in your email to get a private API key.
Add this dependency to your app build.gradle
implementation(libs.coil.compose)
Load an remote image into a AsyncImage view:
@Composable
fun MovieItem() {
val imgUrl = "https://m.media-amazon.com/images/M/MV5BOTlhYTVkMDktYzIyNC00NzlkLTlmN2ItOGEyMWQ4OTA2NDdmXkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg";
AsyncImage(model = imgUrl, contentDescription = "poster")
}
Glide allows for listening to success and error states. This allows you to handle broken urls or to post process images.
Example show Toast if Glide is unable to show image
AsyncImage(model = movie.poster, contentDescription = "poster", onError = {
//Show error message or placeholder
}, onSuccess = {
//Do nothing?!
}, onLoading = {
//Show loading indicator
})