欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android基于Retrofit2.0+RxJava 封装的超好用的工具类

程序员文章站 2022-05-26 17:55:57
...


RetrofitClient

基于Retrofit2.0封装的RetrofitClient.

  • 避免重复创建Retrofit实列.
  • 调用方便简洁.
  • 无需重复设置属性的步骤.
  • 可固定配置 Host 也可动态配置Url、请求头、参数等.
  • 支持文件下载和上传.
  • 支持json形式提交.
  • 支持扩展APIService
  • 统一处理无网络情况,和支持加载进度
  • 结合RxJava
  • 支持缓存机制
  • 优化取消

使用原生的Retrofit请求网络,熟悉的朋友必定了解,在某个ApiServie方法多时 Retrofit设置就显得有点累赘,今天给大家带来对Retrofit的基本封装。这次对Retrofit进阶篇,本次封装已加入RxJava,请在阅读下文前请先了解RXJAVA和本人写的Retrofit系列文章,

基本步骤:

构建Retrofit的接口service.

构建基础拦截器 Interceptor.

构建Cookie管理工具CookieManger.

构建 单列RetrofitClient客户端.

RetrofitClient的使用.

ApiService

请求网络的API接口类,这里你可以增加你需要的请求接口,也可复用已经实现的几个方法。



   /**
* Created by Tamic on 2016-07-08.
*/
 public interface ApiService {

public static final String Base_URL = "http://ip.taobao.com/";
/**
 *普通写法
 */
@GET("service/getIpInfo.php/")
Observable<ResponseBody> getData(@Query("ip") String ip);


@GET("{url}")
Observable<ResponseBody> executeGet(
        @Path("url") String url, 
        @QueryMap Map<String, String> maps);


@POST("{url}")
Observable<ResponseBody> executePost(
        @Path("url") String url,
        @FieldMap Map<String, String> maps);

@Multipart
@POST("{url}")
Observable<ResponseBody> upLoadFile(
        @Path("url") String url,
        @Part("image\\"; filename=\\"image.jpg") RequestBody avatar);

@POST("{url}")
Call<ResponseBody> uploadFiles(
        @Url("url") String url,
        @Part("filename") String description,
        @PartMap()  Map<String, RequestBody> maps);

}

上面新增了几个常用的请求方法

第一个只是普通写法的列子, url ,请求头,参数都是写死的。 不建议这么做

第二,三个分别是Get 和POST请求,method Url, headers, body参数都可以动态外部传入。

四 五是单文件/图片和多文件/图片上传

构建基础拦截器

用来设置基础header,这里是通过MAP键值对来构建,将heder加入到Request中。

  /**
 * BaseInterceptor,use set okhttp call header
 * Created by Tamic on 2016-06-30.
*/
public class BaseInterceptor implements Interceptor{

   private Map<String, String> headers;

   public BaseInterceptor(Map<String, String> headers) {
      this.headers = headers;
    }

   @Override
   public Response intercept(Chain chain) throws    IOException {

    Request.Builder builder = chain.request()
            .newBuilder();
    if (headers != null && headers.size() > 0) {
        Set<String> keys = headers.keySet();
        for (String headerKey : keys) {
            builder.addHeader(headerKey,   headers.get(headerKey)).build();
        }
    }
    return chain.proceed(builder.build());

 }
}

构建Cookie管理者

用来管理cookie, 储存cookie的store这里不再重复说明,具体列子请见:

<Retrofit 2.0 超能实践,完美同步Cookie实现免登录>

public class NovateCookieManger implements CookieJar {

private static final String TAG = "NovateCookieManger";
private static Context mContext;
private static PersistentCookieStore cookieStore;

/**
 * Mandatory constructor for the NovateCookieManger
 */
public NovateCookieManger(Context context) {
    mContext = context;
    if (cookieStore == null) {
        cookieStore = new PersistentCookieStore(mContext);
    }
}

@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
    if (cookies != null && cookies.size() > 0) {
        for (Cookie item : cookies) {
            cookieStore.add(url, item);
        }
    }
}

@Override
public List<Cookie> loadForRequest(HttpUrl url) {
    List<Cookie> cookies = cookieStore.get(url);
    return cookies;
}

}

构建RetrofitClient客户端.

今天重要的环节来了,RetrofitClient主要负责创建具体Retrofit,和调度分发请求。设置格式工厂。添加cookie同步,构建OkHttpClient,添加BaseUrl,对加密证书https我没做加入,希望读者参考我的本系列文章自行加入,因为我不喜欢升伸手党。

   /**
 * RetrofitClient
 * Created by Tamic on 2016-06-15.
 */
public class RetrofitClient {

private static final int DEFAULT_TIMEOUT = 5;

private ApiService apiService;

private OkHttpClient okHttpClient;

public static String baseUrl = ApiService.Base_URL;

private static Context mContext;

private static RetrofitClient sNewInstance;
private static class SingletonHolder {
    private static RetrofitClient INSTANCE = new RetrofitClient(
            mContext);
}

public static RetrofitClient getInstance(Context context) {
    if (context != null) {
        Log.v("RetrofitClient", DevUtil.isDebug() + "");
        mContext = context;
    }
    return SingletonHolder.INSTANCE;
}


public static RetrofitClient getInstance(Context context, String url) {
    if (context != null) {
        mContext = context;
    }
    sNewInstance = new RetrofitClient(context, url);
    return sNewInstance;
}

private RetrofitClient(Context context) {

    this(context, null);
}

private RetrofitClient(Context context, String url) {

    if (TextUtils.isEmpty(url)) {
        url = baseUrl;
    }
    okHttpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(
                    new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
            .cookieJar(new NovateCookieManger(context))
            .addInterceptor(new BaseInterceptor(mContext))
            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .build();
    Retrofit retrofit = new Retrofit.Builder()
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl(url)
            .build();
    apiService = retrofit.create(ApiService.class);
}

public void getData(Subscriber<ResponseBody> subscriber, String ip) {
    apiService.getData(ip)
            .subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
}

public void get(String url, Map headers, Map parameters, Subscriber<ResponseBody> subscriber) {
    apiService.executeGet(url, headers, parameters)
            .subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
}

public void post(String url, Map headers, Map parameters, Subscriber<ResponseBody> subscriber) {
    apiService.executePost(url, headers, parameters)
            .subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
 }

}

细心的朋友已经发现上面代码 在指定生产线程和消费线程的时候,步骤有点麻烦,每个api都得进行指定线程,那么可以利用rxJava的转换器写一个Transformer

Observable.Transformer schedulersTransformer() {
  return new Observable.Transformer() {


  @Override
  public Object call(Object observable) {
   return ((Observable)  observable).subscribeOn(Schedulers.io())
   .unsubscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread());
   }
   };
}

那么api可以这样优化了:

 public Subscription getData(Subscriber<IpResult> subscriber, String ip) {
   return apiService.getData(ip)
   .compose(schedulersTransformer())
   .subscribe(subscriber);
  }

调用 RetrofitClient

   RetrofitClient.getInstance(this).getData(new Subscriber<ResponseBody>() {
        @Override
        public void onCompleted() {
            Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(Throwable e) {
            Toast.makeText(MainActivity.this, "失败!: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNext(ResponseBody ResponseBody) {
            Toast.makeText(MainActivity.this, ResponseBody.toString(), Toast.LENGTH_LONG).show();
        }
    }, "21.22.11.33");

代码很简洁,在用到的地方获取单列直接调用你需要的方法,在RxSubscriber回调中处理你的业务逻辑即可,无需考虑是否在主线程,其他调用方法同上。

很多时候BaseApiService无法满足需求时,Retrofit增加了扩展接口 create 来创建你的API,接着调用execute就可以和RxJava关联

           //create  you APiService    
          MyApiService service =                 

         RetrofitClient.getInstance(MainActivity.this).create(MyApiService.class);    
        // execute and add observable    
        RetrofitClient.getInstance(MainActivity.this).execute(            
                                   service.getData("21.22.11.33"), new Subscriber<IpResult>() {                                     

                          @Override                
                           public void onCompleted() {               
                           } 
           
                           @Override                
                           public void onError(Throwable e) {                    
                                  Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();                             
                           }   
         
                          @Override                
                          public void onNext(IpResult responseBody) {    
                                          Toast.makeText(MainActivity.this, responseBody.toString(),  Toast.LENGTH_LONG).show();                

                        }             
                    });}

取消

 Subscription subscription =   RetrofitClient.getInstance(MainActivity.this)
                    .createBaseApi()
                    .getData(new BaseSubscriber<IpResult>(MainActivity.this) {

                @Override
                public void onError(ResponeThrowable e) {
                    Log.e("Lyk", e.code + " " + e.message);
                    Toast.makeText(MainActivity.this, e.message, Toast.LENGTH_LONG).show();

                }

                @Override
                public void onNext(IpResult responseBody) {
                    Toast.makeText(MainActivity.this, responseBody.toString(), Toast.LENGTH_LONG).show();
                }
            }, "21.22.11.33");
            
            subscription.unsubscribe();

Rxjava结合Retrofit,如何优雅的取消请求!
优雅的取消请看:http://www.jianshu.com/p/d62962243c33

总结

链接:https://github.com/Tamicer/Novate

来源于:  https://www.jianshu.com/p/29c2a9ac5abf



相关标签: RxJava+Retrofit