Well, I am lost in using Retrofit now... At first I write a Singleton helper class which hold a service instance created by a Retrofit instance. It is very convenient to get the service and make HTTP request, but then I find that I can't get the access token from SharedPreferences, because the helper instance is static. Because I use the Authenticator interface to deal with the authentication, it is not possible to pass the access token when making request. I try to extend Application class and hold the Application instance in static field, but Android Studio give me a warning(Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)
).
So now I have another choice: write a static helper method that, for each request, accepts the access token, build a Retrofit instance, create a service instance and make the request. Now I am confusing that whether this is a best practice. What's the difference between reusing one service instance and creating service for each request?
PS: the word service
above refers to the service instance created by someRetrofit.create(someServiceInterface.class)
, not android.app.Service
.