123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package com.example.myapplication.service.api;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import android.text.TextUtils;
- import com.facebook.stetho.common.LogUtil;
- import com.facebook.stetho.okhttp3.BuildConfig;
- import com.facebook.stetho.okhttp3.StethoInterceptor;
- import com.example.myapplication.service.http.interceptor.AppendHeaderAndParameterInterceptor;
- import com.example.myapplication.utils.Constants;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import okhttp3.OkHttpClient;
- import okhttp3.logging.HttpLoggingInterceptor;
- import retrofit2.Retrofit;
- import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class ApiServiceManager {
- //超时时间,3秒
- private static final int CONNECTION_TIME_OUT = 60;
- private static ApiServiceManager instance;
- // private boolean isFileUpload;
- public static ApiServiceManager getInstance() {
- if (instance == null) {
- instance = new ApiServiceManager();
- }
- return instance;
- }
- private ApiServiceManager() {
- super();
- }
- private Map<String, Object> apiServiceMap = new HashMap<>();
- private String apiVersion = Constants.API_VERSION;
- private AppendHeaderAndParameterInterceptor appendHeaderAndParameterInterceptor;
- // private String deviceId =
- // ((TelephonyManager) MyApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE))
- // .getDeviceId();
- private Retrofit retrofit;
- public String getAccessToken() {
- if (null == appendHeaderAndParameterInterceptor) return null;
- return appendHeaderAndParameterInterceptor.getAccessToken();
- }
- public void setAccessToken(String accessToken) {
- if (TextUtils.isEmpty(accessToken)) return;
- if (null == appendHeaderAndParameterInterceptor) return;
- appendHeaderAndParameterInterceptor.setAccessToken(accessToken);
- LogUtil.i("accessToken====" + accessToken);
- }
- public String getApiVersion() {
- return apiVersion;
- }
- // public void isFileUpload(boolean isFile) {
- // LogUtil.d("1111111111 " + appendHeaderAndParameterInterceptor);
- // if (null == appendHeaderAndParameterInterceptor) {
- // return;
- // }
- // appendHeaderAndParameterInterceptor.setFileUpload(isFile);
- // }
- public void setApiVersion(String apiVersion) {
- this.apiVersion = apiVersion;
- }
- /**
- * 获取 API 对应的接口定义文件,并进行相关操作
- */
- public <T> T getApiServiceByClass(Class<T> service) {
- if (null == retrofit) {
- setup();
- }
- String className = service.getName();
- if (apiServiceMap.containsKey(className)) {
- return (T) apiServiceMap.get(className);
- } else {
- T serviceInstance = retrofit.create(service);
- apiServiceMap.put(className, serviceInstance);
- return serviceInstance;
- }
- }
- private void setup() {
- final Gson gson = new GsonBuilder()
- .setDateFormat("yyyy-MM-dd HH:mm:ss")
- .create();
- OkHttpClient okHttpClient = getOkHttpClient();
- retrofit = new Retrofit.Builder().baseUrl(Constants.getBaseUrl()).
- addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
- addConverterFactory(GsonConverterFactory.create(gson)).
- client(okHttpClient).build();
- }
- private OkHttpClient getOkHttpClient() {
- final HttpLoggingInterceptor httpLogginInterceptor = new HttpLoggingInterceptor();
- httpLogginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
- appendHeaderAndParameterInterceptor = new AppendHeaderAndParameterInterceptor();
- // if (isFileUpload) {
- // return new OkHttpClient.Builder()
- // .retryOnConnectionFailure(true)
- // .addInterceptor(httpLogginInterceptor)
- // .build();
- // }
- if (BuildConfig.DEBUG) {
- return new OkHttpClient.Builder()
- .retryOnConnectionFailure(true)
- .addInterceptor(httpLogginInterceptor)
- .connectTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
- .readTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
- .writeTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
- .addInterceptor(appendHeaderAndParameterInterceptor)
- .addNetworkInterceptor(new StethoInterceptor())
- .build();
- } else {
- return new OkHttpClient.Builder()
- .retryOnConnectionFailure(true)
- .addInterceptor(appendHeaderAndParameterInterceptor)
- // .addNetworkInterceptor(new StethoInterceptor())
- .connectTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
- .readTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
- .writeTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
- .build();
- }
- }
- }
|