Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mladenrakonjac/2192011eb8f4901cfee130a6978e8de1 to your computer and use it in GitHub Desktop.
Save mladenrakonjac/2192011eb8f4901cfee130a6978e8de1 to your computer and use it in GitHub Desktop.
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