Retrofit+RXjava的封装+添加OkHttp拦截器日志和缓存

package com.bawei.net;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 作者:$yangxiangrong
 * 

* 2019/4/18 18:58 */ public abstract class ObserverIml implements Observer { @Override public void onSubscribe(Disposable d) { } @Override public abstract void onNext(T t) ; @Override public abstract void onError(Throwable e) ; @Override public void onComplete() { } }

package com.bawei.net;

import java.util.Map;

import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

/**
 * 作者:$yangxiangrong
 * 

* 2019/4/18 19:00 */ public interface HttpService { @GET Observable get(@Url String url, @HeaderMap MaphandMap, @QueryMap Mapmap); @POST Observable post(@Url String url, @HeaderMap MaphandMap, @QueryMap Mapmap); @PUT Observable put(@Url String url, @HeaderMap MaphandMap, @QueryMap Mapmap); }

package com.bawei.net;


import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * 作者:$yangxiangrong
 * 

* 2019/4/18 18:53 */ public class HttpUtils { private String base_url = "http://172.17.8.100"; //更改baseurl public HttpUtils setBaseUrl(String base_url){ this.base_url=base_url; return this; } //传递头参 private Map headMap = new HashMap<>(); public HttpUtils setHead(Map headMap) { this.headMap = headMap; return this; } //get请求 public HttpUtils get(String url, Map map) { if (map == null) { map = new HashMap<>(); } HttpService service = getHttpService(); Observable ob = service.get(url, headMap, map); send(ob); return this; } //post请求 public HttpUtils post(String url, Map map) { HttpService service = getHttpService(); Observable ob = service.post(url, headMap, map); send(ob); return this; } //put请求 public HttpUtils put(String url, Map map) { HttpService service = getHttpService(); Observable ob = service.put(url, headMap, map); send(ob); return this; } //获取请求接口 private HttpService getHttpService() { //添加OkHttp拦截器和缓存路径 File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()); long size=1024*10; OkHttpClient client=new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); return chain.proceed(request); } }).cache(new Cache(file,size)) .build(); Retrofit retrofit = new Retrofit.Builder() .client(client) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl(base_url).build(); return retrofit.create(HttpService.class); } //产生订阅的方法 private void send(Observable ob) { ob.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new ObserverIml() { @Override public void onNext(ResponseBody responseBody) { try { if (cls == null) {//返回字符串 mHttpListener.success(responseBody.string()); } else { //返回JavaBean Object bean = new Gson().fromJson(responseBody.string(), cls); mHttpBeanListener.success(bean); } } catch (Exception e) { e.printStackTrace(); } } @Override public void onError(Throwable e) { if (cls == null) { mHttpListener.fail(e.getMessage()); } else { mHttpBeanListener.fail(e.getMessage()); } } }); } //传递javabean接口 private HttpBeanListener mHttpBeanListener; private Class cls; public void resultBean(Class cls, HttpBeanListener mHttpBeanListener) { this.cls = cls; this.mHttpBeanListener = mHttpBeanListener; } //返回JavaBean public interface HttpBeanListener { void success(T t); void fail(String error); } //传递接口 private HttpListener mHttpListener; public void result(HttpListener mHttpListener) { this.mHttpListener = mHttpListener; } //返回字符串 public interface HttpListener { void success(String data); void fail(String error); } }

注意*******
  implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

你可能感兴趣的:(电商)