Retrofit实用指南

网上关于Retrofit(这里指的是Retrofit2包里的Retrofit)的文章已经普天盖地了!为什么还要写这一篇呢?“扔物线”给了我启发!人们还是喜欢原创的东西,而不是东拼西凑的东西!有价值的东西!当然,现在稍微值钱一点的东西都拿到淘宝卖了!指望“赏”来两个小钱发不了财!当然不是不想你们“赏”钱,而是非常想!因为这是文章价值的体现,和对笔者的充分肯定!

我的笔锋非常犀利,那是写评论文章!现在写的是技术文章,会充分体现我务实求是的人格魅力!没有深奥的东西,也不会故弄玄虚,踏踏实实的人,写踏踏实实的文章。

屁话一大堆了!急性子的人早开流了!言归正传!

一、编写Json数据的Repo类

我用JAVA开发了一个全功能的服务器,除了java库,没有用任何第三方库!今天不打算用我自己的服务器数据!就用Retrofit 官网提供的示例数据!

编写程序前我喜欢看一看json数据是个什么鬼?浏览器打开输入https://api.github.com/users/octocat/repos,显示了下列数据格式,使用Retrofit返回的数据也应该是一样的,所以就写成:

Retrofit 返回的json数据格式为:

[{"id":18221276,"name":"git-consortium","full_name": "octocat/git-consortium","owner": {"login": "octocat","id": 583231,"avatar_url": "https://avatars3.githubusercontent.com/u/583231?v=4",...},...},{...}...]

是一个对象数组,建立对应的POJO类,字段实在太多了!我们利用一小部分字段建一个Repo类,它里面还包含一个Owner类。有人会说字段是否应该完全对应,缺少了行不行?我试验过了,完全可行!我也发表过很多文章,还得过奖,每一篇文章都是自己的实践过程的总结!

  public class Repo {
      public int id;
      public String name;
      public String full_name;
      public Owner owner;
  }

  public class Owner {
      public String login;
      public int id;
      public String avatar_url;
  }

二、Retrofit没有转换器没有调用适配器,还要使用RxJava

看了标题挺吸引人的!是不是吹牛啊?没有真本事就会吹!

我就拿Retrofit的示例演示一下,直接贴代码,你们拷贝粘贴到你们的开发工具中,看看是不是吹牛?

package com.ueuo.retrofit2;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Path;

public class Test3 {
    
    public interface GitHubService {
          @GET("users/{user}/repos")
          Call<ResponseBody> listRepos(@Path("user") String user);
        }
    

    public static void main(String[] args) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .build();
        
        GitHubService service = retrofit.create(GitHubService.class);
        Call<ResponseBody> repos = service.listRepos("octocat");
        
        ResponseBody response = repos.execute().body();
        
        Gson gson =new Gson();
        Type type = new TypeToken<List<Repo>>() {}.getType();
        List<Repo> zla = gson.fromJson(response.charStream(), type);
        
        Observable<List<Repo>> observable = Observable.just(zla);
        observable.flatMap(new Function<List<Repo>, ObservableSource<Repo>>(){

            @Override
            public ObservableSource<Repo> apply(List<Repo> paramT) throws Exception {
                return Observable.fromIterable(paramT);
            }
            
        }).subscribe(new Consumer<Repo>(){
            @Override
            public void accept(Repo repo) throws Exception {
                System.out.println(repo.name);
                System.out.println(repo.full_name);
                System.out.println(repo.owner.avatar_url);
            }           
        });
        
    }

}

我在Eclipse里运行正常,结果是:

git-consortium
octocat/git-consortium
https://avatars3.githubusercontent.com/u/583231?v=4
......

从上面示例可以看出,Retrofit纯粹是脱裤子放屁多次一举!绕了一大圈还是这个结果!不信的话我们就来看看!

三、Retrofit添加Converter和CallAdapter

Retrofit请求数据,以及与RxJava配合使用,需要用到Converter和CallAdapter!这是非常关键的地方,被很多作者忽略,或者不想把真象告诉你们!
要记住Converter是转换json数据到java类,例如Repo类,或者某个类型,例如List<Repo>。
而CallAdapter是把Converter已经转换到的类或者类型,变成RxJava的Observable对象!

我们一起来看示例:

package com.ueuo.retrofit2;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Path;

/**
 * https://api.github.com/users/octocat/repos
 * 
 * @author xbn
 * 
 * 自定义Converter和 CallAdapter
 */
public class Test2 {
    
    public interface GitHubService {
          @GET("users/{user}/repos")
          Observable<List<Repo>> listRepos(@Path("user") String user);
        }
    
    public static class CustomConverter implements Converter<ResponseBody, List<Repo>> {

        public static final CustomConverter INSTANCE = new CustomConverter();
        
        public static CustomConverter create() {
            return INSTANCE;
        }

        @Override
        public List<Repo> convert(ResponseBody value) throws IOException {
            // ResponseBody --> List<Repo>
            Gson gson = new Gson();
            Type type = new TypeToken<List<Repo>>() {}.getType();
            return gson.fromJson(value.charStream(), type);
        }
    }
    
    public static class CustomConverterFactory extends Converter.Factory {

        public static final CustomConverterFactory INSTANCE = new CustomConverterFactory();

        public static CustomConverterFactory create() {
            return INSTANCE;
        }

        // 我们只关实现从 ResponseBody 到  List<Repo> 的转换,所以其它方法可不覆盖
        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            Type rawtype = getRawType(type);            
            if(rawtype == List.class && type instanceof ParameterizedType) {
                return CustomConverter.create();
            }
            //其它类型我们不处理,返回null就行
            return null;
        }
    }
    
    public static class CustomCallAdapter implements CallAdapter<List<Repo>, Observable<List<Repo>>> {

        private final Type responseType;

        //接口方法的返回类型的泛型类型 responseType
        CustomCallAdapter(Type responseType) {
            this.responseType = responseType;
        }

        @Override
        public Type responseType() {
            return responseType;
        }
        
        //Call<List<Repo>> -- Retrofit2的
        public Observable<List<Repo>> adapt(Call<List<Repo>> call) {
            
            Observable<List<Repo>> observable;
            try {
                observable = Observable.just(call.execute().body());
                return observable;
            } catch (IOException e) {
                e.printStackTrace();
            }           
            return null;
        }       
    }

    
    public static class CustomCallAdapterFactory extends CallAdapter.Factory {
        public static final CustomCallAdapterFactory INSTANCE = new CustomCallAdapterFactory();
        
        public static CustomCallAdapterFactory create() {
            return INSTANCE;
        }

        @Override
        public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
            //接口方法的返回类型
            Class<?> rawType = getRawType(returnType);           
            //返回值必须是Observable并且带有泛型
            if (rawType == Observable.class && returnType instanceof ParameterizedType) {
                //接口方法的返回类型的泛型类型
                Type callReturnType = getParameterUpperBound(0, (ParameterizedType) returnType);
                                
                return (CallAdapter<?, ?>) new CustomCallAdapter(callReturnType);
            }
            return null;
        }       
    }

    public static void main(String[] args) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(CustomConverterFactory.create())
                .addCallAdapterFactory(CustomCallAdapterFactory.create())
                .build();
        
        GitHubService service = retrofit.create(GitHubService.class);
        Observable<List<Repo>> repos = service.listRepos("octocat");
        repos.flatMap(new Function<List<Repo>, ObservableSource<Repo>>(){

            @Override
            public ObservableSource<Repo> apply(List<Repo> paramT) throws Exception {
                return Observable.fromIterable(paramT);
            }
            
        }).subscribe(new Consumer<Repo>() {

            @Override
            public void accept(Repo paramT) throws Exception {
                System.out.println(paramT.full_name);
                
            }
            
        });
    

    }

}

拷贝到IDE运行应该输出:

octocat/git-consortium
octocat/hello-worId
octocat/Hello-World
......

关键的地方是:

@Override
        public List<Repo> convert(ResponseBody value) throws IOException {
            // ResponseBody --> List<Repo>
            Gson gson = new Gson();
            Type type = new TypeToken<List<Repo>>() {}.getType();
            return gson.fromJson(value.charStream(), type);
        }

在Converter中Retrofit的请求数据还是原始数据!上面转换成为List<Repo>类型的对象。

然后数据交到CallAdapter:

public Observable<List<Repo>> adapt(Call<List<Repo>> call) {
            
            Observable<List<Repo>> observable;
            try {
                observable = Observable.just(call.execute().body());
                return observable;
            } catch (IOException e) {
                e.printStackTrace();
            }           
            return null;
        }   

可以看到adapt()方法中的参数类型是Call<List<Repo>> !这个类型是Retrofit的或者说是Retrofit2的(我们使用的是Retrofit2,类名还是Retrofit。)有人追究过源码,说是执行实际网络请求的是OkHttp3的Call。我也看了源码,没有追得很深,感觉差不多有那么回事,所以就信了别人的。在这个Call上查到的是一个Retrofit2包里的接口。

四、这些是从哪里学来的?

我非常保守!追求程序的精简,抵触第三方库!现在看来落后了!利用第三方库可以快速轻松的开发软件,已经是一股潮流了!我需要进步!我的Retrofit2的第一任老师是ikidou的“你真的会用Retrofit2吗?Retrofit2完全教程http://www.jianshu.com/p/308f3c54abdd,他还提供了测试服务器RESTServer,非常感谢!同时也要感谢我的不懈努力!它的示例代码,引入Android Studio3.0不行,引入Eclipse也不行,本来打算放弃了,心想还有难倒程序员的?!就试着把源文件拷来拷去,搞定了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容