Retrofit 2 — Play with GET Requests — Beginner Guide — Part 2

A type-safe HTTP client for Android and Java. It makes retrieving JSON or other structured data easily.

Prathamesh More
3 min readJun 6, 2019

Today we are going to play with GET requests.

If you haven't seen part one, please look it up.

GET Request Routes.

https://jsonplaceholder.typicode.com

GET/posts

GET/posts/1

GET/posts/1/comments

GET/comments?postId=1

GET/posts?userId=1

1. /posts

This route already implemented in Part 1.

2. /posts/1

https://jsonplaceholder.typicode.com/posts/1

This URL responds a single JSON object.

In this example, we used a hardcoded string to get a single post.

It’s okay if you just trying.

Let’s understand Java Annotations.

As you know, Java uses certain keywords to describe classes, methods, and variables. For example, a private keyword tells you that the method can only be accessed within the class. When a method has a void return type, it won't return anything. These are structured standard information about your Java code. However, what if you need to supply more structured information, which can't be described with the default keywords?

Annotations are a way to provide additional information (metadata) about packages, interfaces, classes, methods, variables or even parameters. They’re very easy to recognize because they’re always directly before Java keywords and start with an @. Java only supplies a handful of annotations (e.g. @Override and @Deprecated), but supports the creation of custom annotations for a variety of use cases. They can be significantly different in their impact on your project.

The @GET, @POST, @DELETE annotations are actual annotations from the Retrofit library. They're required for all API interface methods since Retrofit needs to know this additional structured data.

Replace hardcoded strings with curly braces {.....}

Now we have to pass a single argument in a methodgetPost() as int postId annotation with @Path("id") .

getPosts(@Path("id") int postId);

getPost() method taking argument so we have to pass id of post

Response from Server

After running this code, you will see the exact output.

What if we have multiple query parameters?

Like this: https://jsonplaceholder.typicode.com/posts?userId=1&_sort=id&_order=desc

Here userId=1, _sort=id sorting by id and order=desc order is descending

Response from Server

Here’s code

If you specify @GET("foobar?a=5"), then any @Query("b") must be appended using &, producing something like foobar?a=5&b=7.

If you specify @GET("foobar"), then the first @Query must be appended using ?, producing something like foobar?b=7.

That’s how Retrofit works.

Next, Code for the MainActivity.java

If you run this code you will get a nice sorted list

Output

Happy coding.

Suggestions are welcome.

Stay tuned for Part 3.

--

--

Prathamesh More
Prathamesh More

Written by Prathamesh More

Passionate, Self-taught Full Stack Web Developer that loves designing and building modern websites, applications, and APIs.

No responses yet