手写简易版Retrofit 附源码

前言

学习源码可以说是一个开发成长的必经之路,本人也是有一直在寻找资源去学习,要是自己硬看的话有那么一点费劲,当然也有很多大佬给过撸源码的技巧,但可能给个人领悟还是差点。。所以本文主要是借鉴了网易在网易云课堂的视频教程写出来的。(跟着视频边写边理解)。

Retrofit

做安卓的应该都清楚,这是一个网络请求的框架 ,主要配合rxjava使用,也是根据okhttp封装的,主要用法这里还是不介绍了,可以自行百度,网上还是很多的,这里我也不多赘述。直接讲一下我对源码的一些理解。

由于是基于okhttp3的,所以需要引入okhttp3的依赖
implementation ‘com.squareup.okhttp3:okhttp:3.11.0’//okhttp3
网络请求的方式也非常简单:

  • 定义接口,定义所有需要调用的接口
public interface ApiServer {

    @GET("/ipJson.jsp")
    Call get(@Query("ip") String ip, @Query("json")String json);
}
  • 调用(实际项目中会将retrofit的基本配置做一个封装,这里就简单演示不封装)
      Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://whois.pconline.com.cn").create();
        ApiServer apiServer = retrofit.create(ApiServer.class);

        Call call = apiServer.get("192.168.1.103","true");

        try {
            Response execute = call.execute();
            System.out.println(execute.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }

以上就是最简单的示例,当然retrofit可以实现的请求功能远远不止于此。这里返回的是okhttp3的call。配合rxjava使用时实际上可以返回一个发射器。本文主要实现上面这个最简单的请求。

搞,搞,搞

首先,要了解核心原理,那就是动态代理。我之前的文章有介绍过。不是很了解的可以先去看看这个动态代理是怎么回事。
动态代理的理解
首先,创建Retrofit类。用建造者模式构建实例:

public class Retrofit {
    //缓存集合  避免同一个接口重复去解读,浪费性能
    private Map<Method, ServiceMethod> serviceMethodCache = new ConcurrentHashMap<>();
    //接口请求的地址
    private HttpUrl baseUrl;
    //发起okhttp请求
    private Call.Factory callFactory;

    private Retrofit(Builder builder) {
        this.baseUrl = builder.baseUrl;
        this.callFactory = builder.callFavtory;
    }

    @SuppressWarnings("unchecked")
    public <T> T create(Class<T> service) {

        return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[]{service}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //拦截某方法的内容,方法名,方法注解值,方法的参数注解值,方法的参数值
                //将信息封装到一个bean类中
                ServiceMethod serviceMethod = loadServiceMethod(method);
                return new OkHttpCall(serviceMethod,args);
            }
        });
    }

    private ServiceMethod loadServiceMethod(Method method) {
        ServiceMethod result = serviceMethodCache.get(method);
        if (result != null){
            return result;
        }
        //同步锁。避免同时调用时都去获取一个新的,这样锁了之后进去再获取一次,确保
        synchronized (this){
            result = serviceMethodCache.get(method);
            if (result == null) {
                //新建bean类的时候需要获取到url,所以需要将retrofit传进去
                result = new ServiceMethod.Builder(this,method).build();
                serviceMethodCache.put(method,result);
            }
        }
        return result;
    }

    public HttpUrl getBaseUrl() {
        return baseUrl;
    }

    public Call.Factory getCallFavtory() {
        return callFactory;
    }

    public static class Builder {

        private HttpUrl baseUrl;
        private Call.Factory callFavtory;

        public Builder baseUrl(HttpUrl httpUrl) {
            if (httpUrl == null) {
                throw new NullPointerException("httpUrl can not be null");
            }
            this.baseUrl = httpUrl;
            return this;
        }

        public Builder baseUrl(String url) {
            if (url.isEmpty()) {
                throw new NullPointerException("url can not be null");
            }
            this.baseUrl = HttpUrl.parse(url);
            return this;
        }

        Builder callFavtory(Call.Factory factory) {
            if (factory == null) {
                throw new NullPointerException("factory can not be null");
            }
            this.callFavtory = factory;
            return this;
        }

       public Retrofit create() {
            if (baseUrl == null) {
                throw new IllegalStateException("null ");
            }
            if (callFavtory == null) {
                callFavtory = new OkHttpClient();
            }
            return new Retrofit(this);
        }
    }
}

其中,比较重要的方法是这个create方法。使用了动态代理,用来获取我们定义的接口的代理,从而去代理这个方法,然后在invoke方法中进行操作。
ServiceMethod实际上就是一个bean类而已。

class ServiceMethod {

    final Call.Factory callFactory; //Okhttp的实现接口
    final HttpUrl httpUrl;//接口请求地址
    private String httpMethod;   //方法的请求方式   get post
    private String relativeUrl;   //方法注解的值
    private ParameterHandler[] parameterHandlers;//方法参数的数组。每个对象包括参数值,参数注解值
    private boolean hasBody;   //是否有请求体


    private ServiceMethod(Builder builder) {
        this.callFactory = builder.retrofit.getCallFavtory();
        this.httpUrl = builder.retrofit.getBaseUrl();
        this.httpMethod = builder.httpMethod;
        this.relativeUrl = builder.relativeUrl;
        this.parameterHandlers = builder.parameterHandlers;
        this.hasBody = hasBody;
    }

    public Call toCall(Object[] args) {
//拼装
        RequestBuilder requestBuilder = new RequestBuilder(httpMethod, httpUrl, relativeUrl, hasBody);
        ParameterHandler[] parameterHandlers = this.parameterHandlers;
        //要做一个检测,如果收集到的参数值数量长度和service里定义的不一样 那样肯定是不行的
        int argumentCount = args != null ? args.length : 0;
        if (argumentCount != parameterHandlers.length){
            throw new IllegalArgumentException("收集参数错误!");
        }
        for (int i = 0; i < argumentCount; i++) {
             parameterHandlers[i].apply(requestBuilder, (String) args[i]);
        }
        return callFactory.newCall(requestBuilder.build());
    }

    static final class Builder {

        private Retrofit retrofit;

        Method method;  //带注解的方法

        Annotation[] methodAnnotations; // 一个方法的所有注解

        Annotation[][] paramsAnnotationArray; //参数的注解,多个参数,一个参数多个注解

        private ParameterHandler[] parameterHandlers;//方法参数的数组。每个对象包括参数值,参数注解值

        private String httpMethod;   //方法的请求方式   get post

        private String relativeUrl;   //方法注解的值

        private boolean hasBody;   //是否有请求体

        //构建时存入数据
        Builder(Retrofit retrofit, Method method) {
            this.retrofit = retrofit;
            this.method = method;
            //方法的所有注解
            this.methodAnnotations = method.getAnnotations();
            //方法参数的所有注解
            this.paramsAnnotationArray = method.getParameterAnnotations();
        }

        ServiceMethod build() {
            //遍历方法的每一个注解  存在get或者post
            for (Annotation methodAnnotation : methodAnnotations) {
                parseMethodAnnotation(methodAnnotation);
            }
            int parameterCount = paramsAnnotationArray.length;  //实际上代表的是外层  参数的个数
            //初始化参数对象数组
            parameterHandlers = new ParameterHandler[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                Annotation[] parameterAnnotations = paramsAnnotationArray[i];   //每一个参数的对应的注解
                if (parameterAnnotations == null) {
                    throw new IllegalArgumentException("参数注解不存在");
                }
                parameterHandlers[i] = parsePrarmeter(parameterAnnotations);
            }

            return new ServiceMethod(this);
        }

        /**
         * 解析每个参数的所有注解
         *
         * @param parameterAnnotations 参数的注解数组
         * @return
         */
        private ParameterHandler parsePrarmeter(Annotation[] parameterAnnotations) {
            ParameterHandler result = null;
            for (Annotation parameterAnnotation : parameterAnnotations) {
                ParameterHandler parameterHandler = parseParameterAnnotation(parameterAnnotation);
                if (parameterHandler == null){
                    continue;
                }
                result = parameterHandler;
            }
            return result;
        }

        /**
         * @param parameterAnnotation 而解析
         * @return
         */
        private ParameterHandler parseParameterAnnotation(Annotation parameterAnnotation) {
            if (parameterAnnotation instanceof Query) {
                Query query = (Query) parameterAnnotation;
                //参数名
                String name = query.value();
                return new ParameterHandler.Query(name);
            } else if (parameterAnnotation instanceof Field) {
                Field query = (Field) parameterAnnotation;
                String name = query.value();
                return new ParameterHandler.Field(name);
            }
            return null;
        }

        private void parseMethodAnnotation(Annotation methodAnnotation) {
            if (methodAnnotation instanceof GET) {
                //解析get方法。最后一个参数是看是否有请求体,get请求是没有请求体的
                parseHttpMethodPath("GET", ((GET) methodAnnotation).value(), false);
            } else if (methodAnnotation instanceof POST) {
                parseHttpMethodPath("POST", ((POST) methodAnnotation).value(), true);
            }
        }

        private void parseHttpMethodPath(String type, String value, boolean hasBody) {
            //将请求的方式赋值
            this.httpMethod = type;
            //("/api/login")
            this.relativeUrl = value;
            this.hasBody = hasBody;
        }
    }
}

这个类中的操作,实际上都是一个一些对注解的封装,使用。然后在retrofit类中,为了避免重复的多次想相同请求导致不断通过反射去获取一系列参数值,做了一个缓存。
注解的定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface GET {
    String value() default "";
}

使用get请求作为示范,实际上也不止这一种,大同小异。
最终的请求拼装类

/**
 * 最终的请求拼装类
 */
public class RequestBuilder {

    //方法的请求方式
    private final String method;
    //接口的请求地址
    private final HttpUrl baseUrl;
    private String relativeUrl;   //方法注解的值  ("/ip/newIp")
    //请求url完整构建者
    private HttpUrl.Builder urlBuilder;
    //form表单构建者
    private FormBody.Builder formBuilder;
    //构建完整请求,包含url method  body
    private final Request.Builder requestBuilder;

    public RequestBuilder(String method, HttpUrl baseUrl, String relativeUrl, boolean hasBody) {
        this.method = method;
        this.baseUrl = baseUrl;
        this.relativeUrl = relativeUrl;
        this.requestBuilder = new Request.Builder();
        if (hasBody) formBuilder = new FormBody.Builder();
    }

    public RequestBuilder(String method, HttpUrl httpUrl, String relativeUrl, HttpUrl.Builder urlBuilder, FormBody.Builder formBuilder, Request.Builder requestBuilder) {
        this.method = method;
        this.baseUrl = httpUrl;
        this.relativeUrl = relativeUrl;
        this.urlBuilder = urlBuilder;
        this.formBuilder = formBuilder;
        this.requestBuilder = requestBuilder;
    }

    public void addQueryParam(String name, String value) {
        if (relativeUrl != null) {
            urlBuilder = baseUrl.newBuilder(relativeUrl);
            if (urlBuilder == null) {
                throw new IllegalArgumentException("url 为空");
            }
            //每次请求都会实例化 需要重置
            relativeUrl = null;
        }
        urlBuilder.addQueryParameter(name, value);
    }

    public void addFieldForm(String name, String value) {
        formBuilder.add(name, value);
    }

    Request build() {
        //定义局部变量   保证每次值都不一样, 易回收
        HttpUrl url;
        if (urlBuilder != null) {
            url = urlBuilder.build();
        } else {
            url = baseUrl.resolve(relativeUrl);
            if (url == null) {
                throw new IllegalArgumentException("build 时url 为空");
            }
        }

        //如果请求中有表单,构建方法中会初始化formBuilder 然后在实例化请求体
        RequestBody body = null;
        if (formBuilder != null) {
            body = formBuilder.build();
        }

        return requestBuilder
                .url(url)
                .method(method, body)
                .build();
    }
}

抽象类和一系列自我实现类,针对每一个参数的注解类型。每一个参数的注解值和参数值 (例如 请求时的参数传递“ip=123.123.123.12"这样)

public abstract class ParameterHandler {

    abstract void apply(RequestBuilder builder, String value);

    static final class Query extends ParameterHandler {
        private String name;
        public Query(String name) {
            this.name = name;
        }

        @Override
        void apply(RequestBuilder builder, String value) {
            if (value == null) {
                return;
            }
            builder.addQueryParam(name, value);
        }
    }

    static final class Field extends ParameterHandler {
        private String name;
        public Field(String name) {
            this.name = name;
        }
        @Override
        void apply(RequestBuilder builder, String value) {
            if (value == null) {
                return;
            }
            builder.addFieldForm(name, value);
        }
    }
}

请求结果Call的封装类:也可以根据自身需要再进行一层的封装。

public class OkHttpCall implements Call {

    private ServiceMethod serviceMethod;
    private Object[] args;
    private Call rawCall;

    public OkHttpCall(ServiceMethod serviceMethod, Object[] args) {
        this.serviceMethod = serviceMethod;
        this.args = args;
        this.rawCall = serviceMethod.toCall(args);
    }

    @Override
    public Request request() {
        return rawCall.request();
    }

    @Override
    public Response execute() throws IOException {
        return rawCall.execute();
    }

    @Override
    public void enqueue(Callback responseCallback) {
        rawCall.enqueue(responseCallback);
    }

    @Override
    public void cancel() {
        rawCall.cancel();
    }

    @Override
    public boolean isExecuted() {
        return rawCall.isExecuted();
    }

    @Override
    public boolean isCanceled() {
        return rawCall.isCanceled();
    }

    @Override
    public Call clone() {
        return rawCall.clone();
    }
}

最简单的retrofit功能就这几个类,就可以实现了。
代码地址:
https://github.com/zhongm15907006125/frameDemo

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值