66 lines
2.0 KiB
Java
66 lines
2.0 KiB
Java
package ch.pizzapp.android.api;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import okhttp3.Interceptor;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
|
import retrofit2.Retrofit;
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
public class ApiService {
|
|
|
|
private static ApiService apiService = new ApiService();
|
|
public static ApiInterface apiInterface;
|
|
public static Retrofit retrofit;
|
|
|
|
private ApiService() { reset();}
|
|
|
|
public static ApiService getInstance() {
|
|
return apiService;
|
|
}
|
|
|
|
public void reset() {
|
|
Gson gson = new GsonBuilder()
|
|
.setLenient()
|
|
.create();
|
|
|
|
|
|
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
|
|
builder.readTimeout(ApiConstants.API_READ_TIMEOUT, TimeUnit.SECONDS);
|
|
builder.connectTimeout(ApiConstants.API_CONNECT_TIMEOUT, TimeUnit.SECONDS);
|
|
|
|
builder.addInterceptor(new Interceptor() {
|
|
@Override
|
|
public Response intercept(Interceptor.Chain chain) throws IOException {
|
|
|
|
Request.Builder builder = chain.request().newBuilder();
|
|
builder.addHeader("Content-Type", "application/json");
|
|
|
|
Request request = builder.build();
|
|
return chain.proceed(request);
|
|
}
|
|
});
|
|
|
|
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); // set your desired log level
|
|
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
|
|
//builder.addInterceptor(logging);
|
|
|
|
OkHttpClient client = builder.build();
|
|
|
|
retrofit = new Retrofit.Builder()
|
|
.baseUrl(ApiConstants.API_PATH)
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
|
.client(client)
|
|
.build();
|
|
|
|
apiInterface = retrofit.create(ApiInterface.class);
|
|
}
|
|
|
|
} |