Finally, after we have initialized the Retrofit object, we can create Retrofit service objects:
public YahooService create() {
return retrofit.create(YahooService.class);
}
This call will take the interface that we have created and turn it into a fully-functioning object using a bit of Java Reflection magic and proxies.
Now we are ready to start making requests. We have intentionally skipped the code of the YahooStockResult class as it will be covered in the next section.
When everything is put in place, this factory class will look as shown:
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitYahooServiceFactory {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new
OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://query.yahooapis.com/v1/public/")
.build();
public YahooService create() {
return retrofit.create(YahooService.class);
}
}