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 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 getApiServiceByClass(Class 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(); } } }