how to create retrofit object.
ReportQuestion
3 years ago 781 views
Retrofit Object Should be create only one for one Application for network Call.
public class ApiClient {
public static final String BASE_URL = "Your Base URL";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
/*HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);*/
OkHttpClient client = new OkHttpClient.Builder()
//.addInterceptor(interceptor)
.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1, Protocol.SPDY_3))
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20000, TimeUnit.SECONDS)
.writeTimeout(10000, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
return retrofit;
}
}
Thread Reply