Skip to content

Instantly share code, notes, and snippets.

@mt-akar
Last active October 14, 2020 19:02
Show Gist options
  • Save mt-akar/c6da2a2af4e3cda88c8da37ff8b961ec to your computer and use it in GitHub Desktop.
Save mt-akar/c6da2a2af4e3cda88c8da37ff8b961ec to your computer and use it in GitHub Desktop.
Simple Retrofit Container
/*
* With help of this class, we reduce the retrofit api call to a single line.
* Example: MyApiContainer.api().getMethod().enqueue(...);
*/
public class MyApiContainer {
interface MyApi {
@GET("someEndpoint")
Call<String> getMethod();
@POST("someEndpoint")
Call<Void> postMethod();
}
// Singleton instance of my custom api interface
static MyApi myApi;
// Return api instance. If not created yet, create it.
public static MyApi api(){
if (myApi == null)
myApi = new Retrofit.Builder()
.baseUrl("https://example.com)
.addConverterFactory(ScalarsConverterFactory.create()) // Order of converter factories is important
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(TestResultsApi.class);
return myApi;
}
}
// Library references
// implementation 'com.squareup.retrofit2:retrofit:2.9.0'
// implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
// implementation 'com.squareup.retrofit2:converter-scalars:2.0.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment