f383e2ab7be8707ec43091f9956e1ce44b15ce89.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.example.myapplication.service.http.interceptor;
  2. import android.text.TextUtils;
  3. import com.facebook.stetho.common.LogUtil;
  4. import com.example.myapplication.MyApplication;
  5. import com.example.myapplication.service.SessionIdService;
  6. import com.example.myapplication.service.UserService;
  7. import com.example.myapplication.utils.AppUtils;
  8. import com.example.myapplication.utils.Constants;
  9. import com.example.myapplication.utils.DeviceUtils;
  10. import com.example.myapplication.utils.SPUtils;
  11. import java.io.IOException;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import okhttp3.HttpUrl;
  15. import okhttp3.Interceptor;
  16. import okhttp3.Request;
  17. import okhttp3.Response;
  18. public class AppendHeaderAndParameterInterceptor implements Interceptor {
  19. //app渠道号
  20. private static final String HEADER_APP_CHANNEL = "channelId";
  21. //app版本名
  22. private static final String HEADER_APP_VERSION = "versionName";
  23. //app版本号
  24. private static final String HEADER_APP_VERSION_CODE = "versionCode";
  25. //app client
  26. public static final String HEADER_APP_CLIENT = "client";
  27. //设备型号
  28. private static final String HEADER_DEVICE_INFO = "model";
  29. //设备版本号
  30. private static final String HEADER_DEVICE_SYSTEM_VERSION = "osVersion";
  31. private String accessToken;
  32. private static Map<String, String> sHeaderMap = new HashMap<>();
  33. @Override
  34. public Response intercept(Chain chain) throws IOException {
  35. Request request = chain.request();
  36. Request.Builder builder = request.newBuilder();
  37. if (null != accessToken) {
  38. builder.addHeader("auth-token", accessToken);
  39. LogUtil.i("auth-token=====" + accessToken);
  40. } else if (!TextUtils.isEmpty(UserService.sAccessToken)) {
  41. builder.addHeader("auth-token", UserService.sAccessToken);
  42. } else {
  43. String token = (String) SPUtils.get(MyApplication.getInstance(), Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Constants.SP_KEY_USER_ACCESS_TOKEN, "");
  44. if (!TextUtils.isEmpty(token)) {
  45. builder.addHeader("auth-token", token);
  46. UserService.sAccessToken = token;
  47. }
  48. }
  49. // addDeviceInfo(builder, HEADER_APP_CHANNEL, HEADER_APP_VERSION, HEADER_DEVICE_INFO, HEADER_DEVICE_SYSTEM_VERSION, HEADER_APP_VERSION_CODE,HEADER_APP_CLIENT);
  50. HttpUrl url = request.url()
  51. .newBuilder()
  52. .addQueryParameter("sessionId", SessionIdService.getSessinId(MyApplication.getInstance()))
  53. // .addQueryParameter("api_version", apiVersion)
  54. // .addQueryParameter("platform", "Android")
  55. .build();
  56. request = builder.url(url).build();
  57. LogUtil.i("url=====" + request.url());
  58. // LogUtil.i("body======" + bodyToString(request));
  59. // LogUtils.i("request headers" + (request.headers()));
  60. Response response = chain.proceed(request);
  61. return response;
  62. }
  63. // private String bodyToString(final Request request) {
  64. // try {
  65. // final Request copy = request.newBuilder().build();
  66. // final Buffer buffer = new Buffer();
  67. // copy.body().writeTo(buffer);
  68. // return buffer.readUtf8();
  69. // } catch (final IOException e) {
  70. // return "did not work";
  71. // }
  72. // }
  73. private void addDeviceInfo(Request.Builder builder, String... params) {
  74. for (String name : params) {
  75. if (sHeaderMap.containsKey(name)) {
  76. builder.addHeader(name, sHeaderMap.get(name));
  77. } else {
  78. String channel = findByName(name);
  79. LogUtil.i(String.format("params name: %s,values: %s", name, channel));
  80. builder.addHeader(name, channel);
  81. sHeaderMap.put(name, channel);
  82. }
  83. }
  84. }
  85. private String findByName(String name) {
  86. String result = "未知";
  87. if (TextUtils.isEmpty(name)) {
  88. return result;
  89. }
  90. if (name.equals(HEADER_APP_CLIENT)) {
  91. return "Android";
  92. }
  93. if (name.equals(HEADER_APP_VERSION)) {
  94. return AppUtils.getAppVersionName(MyApplication.getInstance());
  95. }
  96. if (name.equals(HEADER_APP_VERSION_CODE)) {
  97. return AppUtils.getAppVersionCode(MyApplication.getInstance()) + "";
  98. }
  99. if (name.equals(HEADER_DEVICE_SYSTEM_VERSION)) {
  100. return DeviceUtils.getPhoneSystemVersion();
  101. }
  102. if (name.equals(HEADER_DEVICE_INFO)) {
  103. return DeviceUtils.getPhoneInfo();
  104. }
  105. if (name.equals(HEADER_APP_CHANNEL)) {
  106. return AppUtils.getAppChannel(MyApplication.getInstance());
  107. }
  108. return result;
  109. }
  110. public String getAccessToken() {
  111. return accessToken;
  112. }
  113. // private boolean mIsFileUpload = false;
  114. // public void setFileUpload(boolean fileUpload) {
  115. // this.mIsFileUpload = fileUpload;
  116. // }
  117. public void setAccessToken(String accessToken) {
  118. this.accessToken = accessToken;
  119. }
  120. }