Last active
December 13, 2016 10:11
-
-
Save laaptu/feb3cae85f07c0c5fe3e to your computer and use it in GitHub Desktop.
Singleton class for RetroFit RestAdapter (Retrofit 1.9.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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