美文网首页
Retrofit实用指南

Retrofit实用指南

作者: Professioner | 来源:发表于2017-11-26 01:03 被阅读0次

网上关于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也不行,本来打算放弃了,心想还有难倒程序员的?!就试着把源文件拷来拷去,搞定了!

相关文章

网友评论

      本文标题:Retrofit实用指南

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