package com.example.myapplication.service.http.interceptor; import android.text.TextUtils; import com.facebook.stetho.common.LogUtil; import com.example.myapplication.MyApplication; import com.example.myapplication.service.SessionIdService; import com.example.myapplication.service.UserService; import com.example.myapplication.utils.AppUtils; import com.example.myapplication.utils.Constants; import com.example.myapplication.utils.DeviceUtils; import com.example.myapplication.utils.SPUtils; import java.io.IOException; import java.util.HashMap; import java.util.Map; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; public class AppendHeaderAndParameterInterceptor implements Interceptor { //app渠道号 private static final String HEADER_APP_CHANNEL = "channelId"; //app版本名 private static final String HEADER_APP_VERSION = "versionName"; //app版本号 private static final String HEADER_APP_VERSION_CODE = "versionCode"; //app client public static final String HEADER_APP_CLIENT = "client"; //设备型号 private static final String HEADER_DEVICE_INFO = "model"; //设备版本号 private static final String HEADER_DEVICE_SYSTEM_VERSION = "osVersion"; private String accessToken; private static Map sHeaderMap = new HashMap<>(); @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request.Builder builder = request.newBuilder(); if (null != accessToken) { builder.addHeader("auth-token", accessToken); LogUtil.i("auth-token=====" + accessToken); } else if (!TextUtils.isEmpty(UserService.sAccessToken)) { builder.addHeader("auth-token", UserService.sAccessToken); } else { String token = (String) SPUtils.get(MyApplication.getInstance(), Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Constants.SP_KEY_USER_ACCESS_TOKEN, ""); if (!TextUtils.isEmpty(token)) { builder.addHeader("auth-token", token); UserService.sAccessToken = token; } } // addDeviceInfo(builder, HEADER_APP_CHANNEL, HEADER_APP_VERSION, HEADER_DEVICE_INFO, HEADER_DEVICE_SYSTEM_VERSION, HEADER_APP_VERSION_CODE,HEADER_APP_CLIENT); HttpUrl url = request.url() .newBuilder() .addQueryParameter("sessionId", SessionIdService.getSessinId(MyApplication.getInstance())) // .addQueryParameter("api_version", apiVersion) // .addQueryParameter("platform", "Android") .build(); request = builder.url(url).build(); LogUtil.i("url=====" + request.url()); // LogUtil.i("body======" + bodyToString(request)); // LogUtils.i("request headers" + (request.headers())); Response response = chain.proceed(request); return response; } // private String bodyToString(final Request request) { // try { // final Request copy = request.newBuilder().build(); // final Buffer buffer = new Buffer(); // copy.body().writeTo(buffer); // return buffer.readUtf8(); // } catch (final IOException e) { // return "did not work"; // } // } private void addDeviceInfo(Request.Builder builder, String... params) { for (String name : params) { if (sHeaderMap.containsKey(name)) { builder.addHeader(name, sHeaderMap.get(name)); } else { String channel = findByName(name); LogUtil.i(String.format("params name: %s,values: %s", name, channel)); builder.addHeader(name, channel); sHeaderMap.put(name, channel); } } } private String findByName(String name) { String result = "未知"; if (TextUtils.isEmpty(name)) { return result; } if (name.equals(HEADER_APP_CLIENT)) { return "Android"; } if (name.equals(HEADER_APP_VERSION)) { return AppUtils.getAppVersionName(MyApplication.getInstance()); } if (name.equals(HEADER_APP_VERSION_CODE)) { return AppUtils.getAppVersionCode(MyApplication.getInstance()) + ""; } if (name.equals(HEADER_DEVICE_SYSTEM_VERSION)) { return DeviceUtils.getPhoneSystemVersion(); } if (name.equals(HEADER_DEVICE_INFO)) { return DeviceUtils.getPhoneInfo(); } if (name.equals(HEADER_APP_CHANNEL)) { return AppUtils.getAppChannel(MyApplication.getInstance()); } return result; } public String getAccessToken() { return accessToken; } // private boolean mIsFileUpload = false; // public void setFileUpload(boolean fileUpload) { // this.mIsFileUpload = fileUpload; // } public void setAccessToken(String accessToken) { this.accessToken = accessToken; } }