Leave a rating/review
Notes: 13. Implement a GET Call
The student materials have been reviewed and are updated as of July 2022.
After implementing that POST call to register a user, it’s time to implement a GET call, to fetch the tasks or notes from the server.
But before heading into that, I want to introduce to you a very helpful tool, when it comes to working with APIs, requests and responses - Postman.
Postman is a free tool, that lets you emulate or imitate an application which communicates to a remote resource. You use Postman, to send HTTP requests with various REST methods, to test out an API, before using it. Then you can see what kind of a JSON response the server returns, to build to your Kotlin classes.
You can download Postman for free, by heading over to the official website. You don’t need to install it for the course, but if you want, you can use it to test out any API call before implementing it.
It’s a good practice to test out endpoints with an external tool, and then implement them in code, as it’s easier to get a hang of the API that way. Let’s see how it works.
If you run Postman, you’ll get a welcome screen, where you can add a new request, new collection, environment or API. It’s important to know that Postman has a LOT of useful tools, and you’ll only explore a small bit here.
If you want, you can explore Postman in depth, on your own, but for now, let’s switch to the collection of requests I’ve made, for Taskie.
You can see all the requests here, their respective REST methods, and their names.
Let’s open the Get notes request. You can see that it uses the baseUrl property, and then appends api/note. My base URL ends in a slash, which is why I don’t need one before api.
Furthermore, you can see that there are certain headers available here. In the authorization header, I have my token, so that I can request notes from that user.
After I request the notes, I get the following.
First, you can see the status here, it’s 200, which means OK, and you get the time the request took, and the size of the response. You can also see the response in the JSON format, prettified. It’s an object, holding a property named notes, which is a collection type.
And each item in the collection is an object, which has the following data: id, title, content, taskPriority and isCompleted. So running this request gives you a list of task objects, under the property “notes”, within another object.
With this, you can implement the Get notes request, in the app! Open RemoteApiService.kt & add the following code, for the new request:
@GET("/api/note")
fun getNotes(@Header("Authorization") token: String): Call<ResponseBody>
As before, you’re adding the Authorization header, but using annotations, and returning a ResponseBody. Head over to RemoteApi.kt. Remove the code within the getTasks function, and replace it with the following:
apiService.getNotes(App.getToken()).enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
}
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
}
})
Like previous requests, you enqueue a call, and pass in the the Retrofit callback. Follow up by implementing the success and failure functions:
apiService.getNotes(App.getToken()).enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
onTasksReceived(emptyList(), error)
}
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
val jsonBody = response.body()?.string()
if (jsonBody == null) {
onTasksReceived(emptyList(), NullPointerException("No data available!"))
return
}
val data = gson.fromJson(jsonBody, GetTasksResponse::class.java)
if (data != null && data.notes.isNotEmpty()) {
onTasksReceived(data.notes.filter { !it.isCompleted }, null)
} else {
onTasksReceived(emptyList(), NullPointerException("No data available!"))
}
}
})
In the failure case, you send back the error, and an empty list of notes. In the succcess case, you have to check if there is a response body. If not, you send a NullPointerException, saying there is no data.
If there is data however, you try to parse it using Gson, to the GetTasksResponse, which is structured like the JSON response.
If the parse is successful, and there are notes, you send them back, otherwise, you send back another NPE.
One final thing before requesting the data, go to the NotesFragment, and remove the runOnUiThread call, as you no longer need it!
activity?.runOnUiThread {
}
Now run the project, and check if your notes are visible in the app!
Good job using Postman to test the API, and then implementing a GET call to display your notes! In the next episode, you’ll practice these skills in a fun challenge, see you there! :]