c592341bd0fc65736ba90c80348d1d0e4515a64e.svn-base 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.example.myapplication.service.api;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import android.text.TextUtils;
  5. import com.facebook.stetho.common.LogUtil;
  6. import com.facebook.stetho.okhttp3.BuildConfig;
  7. import com.facebook.stetho.okhttp3.StethoInterceptor;
  8. import com.example.myapplication.service.http.interceptor.AppendHeaderAndParameterInterceptor;
  9. import com.example.myapplication.utils.Constants;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.concurrent.TimeUnit;
  13. import okhttp3.OkHttpClient;
  14. import okhttp3.logging.HttpLoggingInterceptor;
  15. import retrofit2.Retrofit;
  16. import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
  17. import retrofit2.converter.gson.GsonConverterFactory;
  18. public class ApiServiceManager {
  19. //超时时间,3秒
  20. private static final int CONNECTION_TIME_OUT = 60;
  21. private static ApiServiceManager instance;
  22. // private boolean isFileUpload;
  23. public static ApiServiceManager getInstance() {
  24. if (instance == null) {
  25. instance = new ApiServiceManager();
  26. }
  27. return instance;
  28. }
  29. private ApiServiceManager() {
  30. super();
  31. }
  32. private Map<String, Object> apiServiceMap = new HashMap<>();
  33. private String apiVersion = Constants.API_VERSION;
  34. private AppendHeaderAndParameterInterceptor appendHeaderAndParameterInterceptor;
  35. // private String deviceId =
  36. // ((TelephonyManager) MyApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE))
  37. // .getDeviceId();
  38. private Retrofit retrofit;
  39. public String getAccessToken() {
  40. if (null == appendHeaderAndParameterInterceptor) return null;
  41. return appendHeaderAndParameterInterceptor.getAccessToken();
  42. }
  43. public void setAccessToken(String accessToken) {
  44. if (TextUtils.isEmpty(accessToken)) return;
  45. if (null == appendHeaderAndParameterInterceptor) return;
  46. appendHeaderAndParameterInterceptor.setAccessToken(accessToken);
  47. LogUtil.i("accessToken====" + accessToken);
  48. }
  49. public String getApiVersion() {
  50. return apiVersion;
  51. }
  52. // public void isFileUpload(boolean isFile) {
  53. // LogUtil.d("1111111111 " + appendHeaderAndParameterInterceptor);
  54. // if (null == appendHeaderAndParameterInterceptor) {
  55. // return;
  56. // }
  57. // appendHeaderAndParameterInterceptor.setFileUpload(isFile);
  58. // }
  59. public void setApiVersion(String apiVersion) {
  60. this.apiVersion = apiVersion;
  61. }
  62. /**
  63. * 获取 API 对应的接口定义文件,并进行相关操作
  64. */
  65. public <T> T getApiServiceByClass(Class<T> service) {
  66. if (null == retrofit) {
  67. setup();
  68. }
  69. String className = service.getName();
  70. if (apiServiceMap.containsKey(className)) {
  71. return (T) apiServiceMap.get(className);
  72. } else {
  73. T serviceInstance = retrofit.create(service);
  74. apiServiceMap.put(className, serviceInstance);
  75. return serviceInstance;
  76. }
  77. }
  78. private void setup() {
  79. final Gson gson = new GsonBuilder()
  80. .setDateFormat("yyyy-MM-dd HH:mm:ss")
  81. .create();
  82. OkHttpClient okHttpClient = getOkHttpClient();
  83. retrofit = new Retrofit.Builder().baseUrl(Constants.getBaseUrl()).
  84. addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
  85. addConverterFactory(GsonConverterFactory.create(gson)).
  86. client(okHttpClient).build();
  87. }
  88. private OkHttpClient getOkHttpClient() {
  89. final HttpLoggingInterceptor httpLogginInterceptor = new HttpLoggingInterceptor();
  90. httpLogginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  91. appendHeaderAndParameterInterceptor = new AppendHeaderAndParameterInterceptor();
  92. // if (isFileUpload) {
  93. // return new OkHttpClient.Builder()
  94. // .retryOnConnectionFailure(true)
  95. // .addInterceptor(httpLogginInterceptor)
  96. // .build();
  97. // }
  98. if (BuildConfig.DEBUG) {
  99. return new OkHttpClient.Builder()
  100. .retryOnConnectionFailure(true)
  101. .addInterceptor(httpLogginInterceptor)
  102. .connectTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
  103. .readTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
  104. .writeTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
  105. .addInterceptor(appendHeaderAndParameterInterceptor)
  106. .addNetworkInterceptor(new StethoInterceptor())
  107. .build();
  108. } else {
  109. return new OkHttpClient.Builder()
  110. .retryOnConnectionFailure(true)
  111. .addInterceptor(appendHeaderAndParameterInterceptor)
  112. // .addNetworkInterceptor(new StethoInterceptor())
  113. .connectTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
  114. .readTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
  115. .writeTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
  116. .build();
  117. }
  118. }
  119. }