借助泛型实现了复用,请求时可以比普通复用少写一点代码(滑稽脸),两种工具类都采用的单例模式
普通的工具类因为必须知道具体的类型(url)才可以创建API的实体类,所以每增加一个url就要进来修改一次,即使只有一部分不同(在同一个接口中)也需要进来修改,非常僵硬麻烦,下面只贴了工具类
import androID.util.Log;import com.example.practice.API.ImageAPI;import com.example.practice.API.ServiceAPI;import com.example.practice.model.data.HomeData;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;import io.reactivex.Flowable;import okhttp3.Interceptor;import okhttp3.OkhttpClIEnt;import okhttp3.Request;import okhttp3.Response;import retrofit2.Retrofit;import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;import retrofit2.converter.gson.GsonConverterFactory;import static com.example.practice.API.ServiceAPI.DATA_HOME;public class httpManager { private static httpManager instance;//单例模式 public static httpManager getInstance() { if (instance == null) { synchronized (httpManager.class) { if (instance == null) { instance = new httpManager(); } } } return instance; } private ServiceAPI homeAPI; private String baseUrl = DATA_HOME; private ImageAPI imageAPI; private Map<String,Retrofit> map = new HashMap<>(); private Retrofit getRetrofit(String url) { return new Retrofit.Builder().baseUrl(url) .clIEnt(getoKhttpClIEnt()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); } private OkhttpClIEnt getoKhttpClIEnt() { return new OkhttpClIEnt.Builder() .connectTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .addInterceptor(new LoggingInterceptor()) .build(); } private class LoggingInterceptor implements Interceptor { @OverrIDe public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Log.e("TAG", "intercept: 日志拦截器" ); Response proceed = chain.proceed(request); Log.e("TAG", "intercept: 请求后" ); return proceed; } } public ServiceAPI getHomeAPI() { if (homeAPI == null) { homeAPI = getRetrofit(baseUrl).create(ServiceAPI.class); } return homeAPI; } public ImageAPI getimageAPI(String baseUrl) { Retrofit retrofit = map.get(baseUrl); if(retrofit !=null){ imageAPI = retrofit.create(ImageAPI.class); }else{ retrofit = getRetrofit(baseUrl); imageAPI = retrofit.create(ImageAPI.class); map.put(baseUrl,retrofit); } return imageAPI; }}
使用泛型的工具类因为普通的工具类是因为必须需要具体的类型才导致的僵硬,那么我们使用泛型让它变得不需要具体的类型就可以更灵活了,这里是配合MVP使用的。
工具类public class RetrofitUtils implements INetWorkInterface { private static RetrofitUtils retrofitUtils; private APIService APIService; private RetrofitUtils() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(URLConstant.BASEURL) .clIEnt(getoKhttpClIEnt()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); APIService = retrofit.create(APIService.class); } private OkhttpClIEnt getoKhttpClIEnt() { return new OkhttpClIEnt.Builder() .connectTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .addInterceptor(new RetrofitUtils.LoggingInterceptor()) .build(); } private class LoggingInterceptor implements Interceptor { @OverrIDe public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Log.e("TAG", "intercept: 日志拦截器"); Response proceed = chain.proceed(request); Log.e("TAG", "intercept: 请求后"); return proceed; } } public static RetrofitUtils getInstance() { if (retrofitUtils == null) { synchronized (RetrofitUtils.class) { if (retrofitUtils == null) { retrofitUtils = new RetrofitUtils(); } } } return retrofitUtils; } @OverrIDe public <T> voID get(String url, INetCallBack<T> callBack) { APIService.get(url) .compose(RxUtils.changeScheduler()) .subscribe(new Observer<ResponseBody>() { @OverrIDe public voID onSubscribe(@NonNull disposable d) { } @OverrIDe public voID onNext(@NonNull ResponseBody responseBody) { try { String string = responseBody.string(); Type[] genericInterfaces = callBack.getClass().getGenericInterfaces(); Type[] actualTypeArguments = ((ParameterizedType) genericInterfaces[0]).getActualTypeArguments(); Type t = actualTypeArguments[0]; T result = new Gson().fromJson(string, t); callBack.onSuccess(result); } catch (IOException e) { e.printstacktrace(); } } @OverrIDe public voID one rror(@NonNull Throwable e) { } @OverrIDe public voID onComplete() { } }); } @OverrIDe public <T> voID post(String url, INetCallBack<T> callBack) { } @OverrIDe public <T> voID post(String url, HashMap<String, String> map, INetCallBack<T> callBack) { }}
工具类中使用到的接口和类统一切换线程
public class RxUtils { /** * rxjava 线程切换统一处理 * * @param <T> * @return */ public static <T> Flowabletransformer<T, T> rxScheduler() { return new Flowabletransformer<T, T>() { @OverrIDe public Publisher<T> apply(Flowable<T> upstream) { return upstream.subscribeOn(Schedulers.io()) .observeOn(AndroIDSchedulers.mainThread()); } }; } public static <T> Observabletransformer<T, T> changeScheduler() { return new Observabletransformer<T, T>() { @OverrIDe public ObservableSource<T> apply(Observable<T> upstream) { return upstream.subscribeOn(Schedulers.io()) .observeOn(AndroIDSchedulers.mainThread()); } }; }}
使用的路径和retrofit接口
public interface URLConstant { String BASEURL = "http://cDWan.cn:7000/exam2003/"; String BANNER = "abannerList.Json"; String left = "anewsList.Json"; String RIGHT = "astudent.Json";}
public interface APIService { @GET Observable<ResponseBody> get(@Url String url); @FormUrlEncoded @POST Observable<ResponseBody> post(@Url String url); @FormUrlEncoded @POST Observable<ResponseBody> post(@Url String url, @FIEldMap HashMap<String,String> map);}
使用 RetrofitUtils.getInstance().get(url, callBack);
总结 以上是内存溢出为你收集整理的Android Rxjava+Retrofit网络请求框架 使用工具类优化全部内容,希望文章能够帮你解决Android Rxjava+Retrofit网络请求框架 使用工具类优化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)