Created
September 13, 2016 08:06
-
-
Save mladenrakonjac/2192011eb8f4901cfee130a6978e8de1 to your computer and use it in GitHub Desktop.
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 com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import okhttp3.OkHttpClient; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
/** | |
* Created by mladenrakonjac on 01/06/16. | |
*/ | |
public class ServiceGenerator { | |
public static String apiBaseUrl = Constants.BASE_API_URL; | |
private static Retrofit retrofit; | |
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); | |
private static Gson gson = new GsonBuilder() | |
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss") | |
.create(); | |
private static Retrofit.Builder builder = | |
new Retrofit.Builder() | |
.baseUrl(apiBaseUrl) | |
.addConverterFactory(GsonConverterFactory.create(gson)); | |
public static void changeApiBaseUrl(String newApiBaseUrl) { | |
apiBaseUrl = newApiBaseUrl; | |
builder = new Retrofit.Builder() | |
.addConverterFactory(GsonConverterFactory.create()) | |
.baseUrl(apiBaseUrl); | |
} | |
public static <S> S createService(Class<S> serviceClass) { | |
Retrofit retrofit = builder.client(httpClient.build()).build(); | |
return retrofit.create(serviceClass); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment