Retrofit+RxJava
来源:互联网 发布:大卖家淘宝流量互刷 编辑:程序博客网 时间:2024/06/05 09:43
public interface ZhihuApi { @GET("/api/4/news/latest") Observable<ZhihuDaily> getLastDaily(); @GET("/api/4/news/before/{date}") Observable<ZhihuDaily> getTheDaily(@Path("date") String date); @GET("/api/4/news/{id}") Observable<ZhihuStory> getZhihuStory(@Path("id") String id); @GET("http://lab.zuimeia.com/wallpaper/category/1/?page_size=1") Observable<ImageResponse> getImage(); @GET("http://caiyao.name/releases/MrUpdate.json") Observable<UpdateItem> getUpdateInfo();}
请求封装
public class ZhihuRequest { private static Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR=new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { if (NetWorkUtil.isNetWorkAvailable(MicroApplication.getContext())){ int maxAge = 60; Response response=chain.proceed(chain.request()); return response.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", "public, max-age=" + maxAge) .build(); }else{ int maxAge = 60*60*24*28; Response response=chain.proceed(chain.request()); return response.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", "public, max-age=" + maxAge) .build(); } } }; public static File cacheFile=new File(MicroApplication.getContext().getCacheDir(),"zhihuCache"); private static int cacheSize = 10 * 1024 * 1024; public static Cache cache=new Cache(cacheFile,cacheSize); public static OkHttpClient client=new OkHttpClient.Builder() .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .cache(cache).build(); private static ZhihuApi zhihuApi = null; public static ZhihuApi getZhihuApi(){ synchronized (ZhihuApi.class){ if (zhihuApi==null){ zhihuApi= new Retrofit.Builder() .baseUrl("http://news-at.zhihu.com") .addCallAdapterFactory( RxJavaCallAdapterFactory.create()) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build().create(ZhihuApi.class); } } return zhihuApi; }}
调用
ZhihuRequest.getZhihuApi().getImage().subscribeOn(Schedulers.io()) .map(new Func1<ImageResponse, Boolean>() { @Override public Boolean call(ImageResponse imageResponse) { return true; } }).observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<Boolean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Boolean aBoolean) { } });
1 0
- Retrofit+Rxjava
- Retrofit+RxJava
- Retrofit+RxJava
- Retrofit+RxJava
- Retrofit+RxJava
- RxJava+Retrofit
- rxjava+retrofit
- RxJava&Retrofit
- Retrofit+Rxjava
- Retrofit+RxJava
- RXJava & Retrofit
- Rxjava+retrofit
- rxjava+retrofit
- Rxjava & Retrofit
- retrofit+Rxjava
- Retrofit + RxJava
- RxJava + Retrofit
- retrofit-rxjava封装、mvp-retrofit-rxjava demo
- 模拟实现memset()
- virtualbox共享文件夹无访问权限问题解决方法
- 动态计算UITableViewCell高度详解
- HTML5 requestFullScreen&exitFullscreen全屏兼容方案
- 使用NASM和CL(或LINK)写HelloWorld示例
- Retrofit+RxJava
- [从codewars学习到的JS系列1]数组方法reduce(),map(),正则$1等的使用
- Github 徽章整理
- Android 源码 crash 隐患静态扫描工具--GodEyes-android介绍
- RPC原理详解
- Chrome 开发工具之Network
- html进制转换
- 正则表达式
- 在mac上加速android studio