Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Singleton class for RetroFit RestAdapter (Retrofit 1.9.0)
import retrofit.RestAdapter;
/**
* Singleton class for Retrofit RestAdapter and API (Retrofit 1.9.0)
*/
public class ApiManager {
// interface containing HTTP methods as given by Retrofit
private static RegionApi regionApi;
// static adapter to be used in entire app
static volatile RestAdapter restAdapter = null;
//dynamic endpoints if needed to change base url
private static DynamicEndPoint dynamicEndPoint;
private ApiManager() {
}
// singleton for RestAdapter
public static RestAdapter getAdapter() {
if (restAdapter == null) {
synchronized (ApiManager.class) {
if (restAdapter == null) {
dynamicEndPoint = new DynamicEndPoint("http://somebase url");
restAdapter = new RestAdapter.Builder().setEndpoint(dynamicEndPoint).setLogLevel(RestAdapter.LogLevel.FULL)
.build();
}
}
}
return restAdapter;
}
/**Method to get url used by DynamicEndPoint*/
public static String getBaseUrl() {
getAdapter();
return dynamicEndPoint.getUrl();
}
/**Method to set Base url for our endpoint*/
public static void setBaseUrl(String url) {
getAdapter();
synchronized (ApiManager.class) {
dynamicEndPoint.changeEndPoint(url);
}
}
/**Initializing Singleton for RegionApi*/
public static void initRegionApi() {
if (regionApi == null) {
synchronized (ApiManager.class) {
if (regionApi == null) {
regionApi = getAdapter().create(RegionApi.class);
}
}
}
}
/**Method to get RegionApi*/
public static RegionApi getRegionApi() {
initRegionApi();
return regionApi;
}
/**Method to get RegionApi with changed base url*/
public static RegionApi getRegionApi(String baseUrl) {
initRegionApi();
setBaseUrl(baseUrl);
return regionApi;
}
}
public class DynamicEndPoint implements Endpoint {
//http://stackoverflow.com/a/27816635/739306
//http://stackoverflow.com/a/23279628/739306
//Retrofit 1.9.0
String baseUrl;
public DynamicEndPoint(String baseUrl) {
this.baseUrl = baseUrl;
}
public void changeEndPoint(String baseUrl) {
this.baseUrl = baseUrl;
}
@Override
public String getUrl() {
return baseUrl;
}
@Override
public String getName() {
return "default";
}
}
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Path;
import retrofit.http.Query;
/**
* Retrofit 1.9.0
* Simple interface for HTTP methods exposed by Retrofit
*/
public interface RegionApi {
@GET("/regions")
public void fetchRegions(Callback<RegionList> callback);
}
@ep372

This comment has been minimized.

Copy link

commented Jun 27, 2016

Hi there!
Thank you for the nice example. It's fine for me :)
I have just a question :
Could you explain why you need volatile with the restAdapter property? Is it only because of the DynamicEndpoint which can be change across threads? May I omit it if do not use a dynamic endpoint ?
Thanks for your explanation
DDP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.