http://blog.csdn.net/a1240373779/article/details/78592993 添加公共请求参数 public class MyInterceptor implements Interceptor { public static String uid; public static String token; private int versioncode; public Response intercept(Interceptor.Chain chain) throws IOException { SpUtils utils=new SpUtils(App.context,"Login"); token = utils.getString("token", ""); uid = utils.getString("uid", ""); PackageManager manager = App.context.getPackageManager(); PackageInfo info = null; try { info = manager.getPackageInfo(App.context.getPackageName(),0); versioncode = info.versionCode; } catch (Exception e) { e.printStackTrace(); } Request request=chain.request(); //判断当前的请求 if (request.method().equals("POST")){ //判断当前的请求Boby if (request.body() instanceof FormBody){ //创建一个新的FromBoby FormBody.Builder bobyBuilder = new FormBody.Builder(); //获取原先的boby FormBody body = (FormBody) request.body(); //遍历boby for (int i = 0; i < body.size(); i++) { //取出原先boby的数据 存入新的boby里 bobyBuilder.add(body.encodedName(i),body.encodedValue(i)); } System.out.println("===token======"+token); body=bobyBuilder.addEncoded("token",token) .addEncoded("source","android") .addEncoded("appVersion", String.valueOf(versioncode)) .build(); request=request.newBuilder().post(body).build(); } else { MultipartBody body= (MultipartBody) request.body(); MultipartBody.Builder builder=new MultipartBody.Builder().setType(MultipartBody.FORM); builder.addFormDataPart("token",token); builder.addFormDataPart("source","android"); builder.addFormDataPart("appVersion",String.valueOf(versioncode)); List<MultipartBody.Part> parts=body.parts(); for (MultipartBody.Part part : parts) { builder.addPart(part); } request=request.newBuilder().post(builder.build()).build(); } } return chain.proceed(request); } }
网络请求公共参数拦截器
最新推荐文章于 2022-11-20 20:26:16 发布