• 自定义拦截器


    MyInterceptor post

    public class MyInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            //获取原始的请求数据
            Request orginRequest = chain.request();
            //先获取原始的请求参数,然后再拼接起来
            FormBody orginBody = (FormBody) orginRequest.body();
            FormBody.Builder builder = new FormBody.Builder();
            for (int i = 0; i < orginBody.size(); i++) {
                String name = orginBody.name(i);
                String value = orginBody.value(i);
                builder.add(name, value);
            }
            builder.add("source", "android");
            FormBody formBody = builder.build();
            Request request = new Request.Builder().url(orginRequest.url()).post(formBody).build();
    
            return chain.proceed(request);
    
        }
    }
    

     get自定义拦截器

    public class MyInterceptor implements Interceptor{
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url();
            String string = url.url().toString();
            String resultUrl = string+"&source=android";
    
            Request request1 = request.newBuilder().url(resultUrl).build();
    
            return chain.proceed(request);
        }
    }

    网络请求类

    public class HttpUtils {
        private static HttpUtils httpUtils;
        private final OkHttpClient client;
    
        private HttpUtils() {
            //创建OkHttpClient
            //创建拦截器
    
            client = new OkHttpClient.Builder()
                    .addInterceptor(new MyInterceptor())
                    .build();
        }
    
        public static HttpUtils getHttpUtils() {
            if (httpUtils == null) {
                synchronized (HttpUtils.class) {
                    if (httpUtils == null) {
                        httpUtils = new HttpUtils();
                    }
                }
            }
            return httpUtils;
        }
    
        /**
         * GET请求
         *
         * @param url      请求地址
         * @param callback 回调
         */
        public void doGet(String url, Callback callback) {
            Request request = new Request.Builder().url(url).build();
            client.newCall(request).enqueue(callback);
        }
    
        /**
         * POST请求
         *
         * @param url      请求地址
         * @param params   请求的参数
         * @param callback 回调
         */
        public void doPost(String url, Map<String, String> params, Callback callback) {
            //这里可以加网络判断
    
            //判断参数
            if (params == null || params.size() == 0) {
                throw new RuntimeException("params is null!!!");
            }
            // 创建Request
            FormBody.Builder builder = new FormBody.Builder();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                builder.add(entry.getKey(), entry.getValue());
            }
            FormBody formBody = builder.build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();
            client.newCall(request).enqueue(callback);
        }
    }
    

      

  • 相关阅读:
    ADLINK 8158控制程序-连续运动(VB.NET)
    .NET通信中的同步和异步处理
    Java 数据类型转换(转换成字节型)
    matlab中的三维坐标系与旋转
    maya和Unity中的坐标系旋转
    Android Volley完全解析(一),初识Volley的基本用法
    基于VLC的视频播放器(转载)
    android超快模拟器Ggenymotion的安装和配置
    真机在wifi下调试android程序
    Android图片异步加载框架Android-Universal-Image-Loader
  • 原文地址:https://www.cnblogs.com/wsq110/p/7874890.html
Copyright © 2020-2023  润新知