美文网首页
retrofit解析

retrofit解析

作者: Skypew | 来源:发表于2017-12-07 15:49 被阅读12次

    底层也是 okhttp

    使用方法

      //设置baseUrl
            Retrofit retrofit = new Retrofit.Builder()
                     .client(client)//可用来设置okhttp
                    .baseUrl("http://www.baidu.com")
                    .build();
    
            RemoteService  remoteService = retrofit.create(RemoteService.class);
    
            Call<RspModel<List<GroupCard>>> call = remoteService.groupSearch("群组");
            call.enqueue(new Callback<RspModel<List<GroupCard>>>() {
                @Override
                public void onResponse(Call<RspModel<List<GroupCard>>> call, Response<RspModel<List<GroupCard>>> response) {
    
                }
    
                @Override
                public void onFailure(Call<RspModel<List<GroupCard>>> call, Throwable t) {
    
                }
            });
    
    
    public interface RemoteService {
    
     // 群搜索的接口
        @GET("group/search/{name}")
        Call<RspModel<List<GroupCard>>> groupSearch(@Path(value = "name", encoded = true) String name);
    
    }
    
    

    源码分析

    动态代理
    1. 首先通过method 把它转化为ServiceMethod;
    2. 然后,通过serviceMethod,arg获取到okHttpCall对象;
    3. 最后,再把okHttpCall进一步封装成Call对象

    build()源码解析


    image.png image.png image.png

    相关文章

      网友评论

          本文标题:retrofit解析

          本文链接:https://www.haomeiwen.com/subject/gibdixtx.html