Service (endpoint) interfaces are the core things when working with Retrofit library. Basically, they let you define the structure and parameters of HTTP requests by putting metadata on a simple Java Class interface.
Let's take a look at this example that we will use to query financial stocks:
import io.reactivex.Single;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface YahooService {
@GET("yql?format=json")
Single<YahooStockResult> yqlQuery(
@Query("q") String query,
@Query("env") String env
);
}
Just by taking a quick look at this HTTP interface definition, we can already tell a few things:
- There will be just a single object as a response of the YahooStockResult type
- The first query parameter is format, and it's set to json
- The part of the path that will be queried is yql
- Two parameters are specified by the caller: q--the query and env--data source table
If we want to make the same request as mentioned, we would need to set q (query) to the following:
String query = "select * from yahoo.finance.quote where symbol in ('YHOO','AAPL','GOOG','MSFT')";
Also, env is to be set to the following:
String env = "store://datatables.org/alltableswithkeys";
All the encoding that's required for special symbols such as / and others will be handled by Retrofit during the execution.