├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── werb │ │ └── gankwithzhihu │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── fonts │ │ │ ├── dqht.ttf │ │ │ └── rm_albion.ttf │ ├── java │ │ └── com │ │ │ └── werb │ │ │ └── gankwithzhihu │ │ │ ├── MyApp.java │ │ │ ├── api │ │ │ ├── ApiService.java │ │ │ ├── Constant.java │ │ │ ├── OkHttpManager.java │ │ │ └── services │ │ │ │ ├── DailyApi.java │ │ │ │ ├── GankApi.java │ │ │ │ └── ZhihuApi.java │ │ │ ├── bean │ │ │ ├── daily │ │ │ │ ├── Banners.java │ │ │ │ ├── Category.java │ │ │ │ ├── Daily.java │ │ │ │ ├── DailyTimeLine.java │ │ │ │ ├── HeadLine.java │ │ │ │ ├── Meta.java │ │ │ │ ├── Options.java │ │ │ │ ├── Post.java │ │ │ │ └── Response.java │ │ │ ├── gank │ │ │ │ ├── Gank.java │ │ │ │ ├── GankData.java │ │ │ │ ├── Meizhi.java │ │ │ │ └── Video.java │ │ │ └── zhihu │ │ │ │ ├── News.java │ │ │ │ ├── NewsTimeLine.java │ │ │ │ ├── SplashImage.java │ │ │ │ ├── Stories.java │ │ │ │ └── TopStories.java │ │ │ ├── ui │ │ │ ├── activity │ │ │ │ ├── AboutMeActivity.java │ │ │ │ ├── DailyFeedActivity.java │ │ │ │ ├── GankActivity.java │ │ │ │ ├── GankWebActivity.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── PictureActivity.java │ │ │ │ ├── SplashActivity.java │ │ │ │ └── ZhihuWebActivity.java │ │ │ ├── adapter │ │ │ │ ├── DailyFeedAdapter.java │ │ │ │ ├── DailyListAdapter.java │ │ │ │ ├── GankActivityAdapter.java │ │ │ │ ├── GankListAdapter.java │ │ │ │ ├── ViewPagerFgAdapter.java │ │ │ │ └── ZhihuListAdapter.java │ │ │ ├── base │ │ │ │ ├── BasePresenter.java │ │ │ │ ├── MVPBaseActivity.java │ │ │ │ └── MVPBaseFragment.java │ │ │ ├── fragment │ │ │ │ ├── DailyFragment.java │ │ │ │ ├── GankFragment.java │ │ │ │ └── ZhihuFragment.java │ │ │ ├── presenter │ │ │ │ ├── DailyFeedPresenter.java │ │ │ │ ├── DailyFgPresenter.java │ │ │ │ ├── GankFgPresenter.java │ │ │ │ ├── GankPresenter.java │ │ │ │ ├── GankWebPresenter.java │ │ │ │ ├── SplashPresenter.java │ │ │ │ ├── ZhihuFgPresenter.java │ │ │ │ └── ZhihuWebPresenter.java │ │ │ └── view │ │ │ │ ├── IDailyFeedView.java │ │ │ │ ├── IDailyFgView.java │ │ │ │ ├── IGankFgView.java │ │ │ │ ├── IGankView.java │ │ │ │ ├── IGankWebView.java │ │ │ │ ├── ISplashView.java │ │ │ │ ├── IZhihuFgView.java │ │ │ │ └── IZhihuWebView.java │ │ │ ├── util │ │ │ ├── DateUtils.java │ │ │ ├── ScreenUtil.java │ │ │ ├── StateUtils.java │ │ │ └── StringUtils.java │ │ │ └── widget │ │ │ ├── SplashView.java │ │ │ └── TopStoriesViewPager.java │ └── res │ │ ├── drawable-xxhdpi │ │ ├── android.png │ │ ├── feed_0_icon.png │ │ ├── feed_1_icon.png │ │ ├── headline.png │ │ ├── headline_tag.png │ │ ├── ic_file_download_white_24dp.png │ │ ├── ic_play_arrow_white_24dp.png │ │ ├── ios.png │ │ ├── me_bg.jpeg │ │ ├── normal.png │ │ ├── selected.png │ │ ├── tuijian.png │ │ ├── tuozhan.png │ │ ├── video.png │ │ └── web.png │ │ ├── drawable │ │ └── border_bottom.xml │ │ ├── layout │ │ ├── activity_about_me.xml │ │ ├── activity_daily_feed.xml │ │ ├── activity_gank.xml │ │ ├── activity_gank_web.xml │ │ ├── activity_main.xml │ │ ├── activity_pic.xml │ │ ├── activity_splash.xml │ │ ├── activity_view_footer.xml │ │ ├── activity_view_toolbar.xml │ │ ├── activity_web_view.xml │ │ ├── fragment_daily.xml │ │ ├── fragment_gank.xml │ │ ├── fragment_zhihu.xml │ │ ├── item_daily_feed_0.xml │ │ ├── item_daily_feed_1.xml │ │ ├── item_daily_feed_option.xml │ │ ├── item_daily_headline.xml │ │ ├── item_empty.xml │ │ ├── item_gank_list.xml │ │ ├── item_gank_meizi.xml │ │ ├── item_zhihu_stories.xml │ │ └── item_zhihu_top_stories.xml │ │ ├── menu │ │ └── main_menu.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── icon.png │ │ └── wb_sunny.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── werb │ └── gankwithzhihu │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots ├── aboutme.png ├── dailt_feed.png ├── daily_detail.png ├── daily_list.png ├── daily_list_2.png ├── gank_detial.png ├── gank_list.png ├── gank_meizhi.png ├── maindetail.png ├── splash.png ├── zhigan.png ├── zhihu_fg.png └── zhihu_web.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | 22 | # Android Studio 23 | .idea/ 24 | .gradle 25 | /*/local.properties 26 | /*/out 27 | /*/*/build 28 | /*/*/production 29 | *.iml 30 | *.iws 31 | *.ipr 32 | *~ 33 | *.swp 34 | 35 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/werbhelius/GankWithZhihu/47181b9c309c75150c3484293294a447b6f9bb74/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 实践!业余时间做的一款阅读类App (MVP + RxJava + Retrofit) 3 | 4 | [APK下载地址](https://fir.im/bew6) 或 [Releases](https://github.com/Werb/GankWithZhihu/releases) 5 | 6 | * 整体项目基于 MVP + RxJava + Retrofit 7 | * 通过 Retrofit 实现了无网缓存 8 | * 基于 MVP 模式对 Activity 和 Fragment 封装了两个基类,同样适用于非 MVP 的实现。 9 | * 运用 RecyclerView 加载了多种复杂布局 10 | * 用到了一些很棒的第三方库 11 | * GitHub 项目地址 : [https://github.com/Werb/GankWithZhihu](https://github.com/Werb/GankWithZhihu) 12 | 13 | 14 | ### 闪屏 15 | * 实现了类似于 Twitter 的闪屏动画,中间的小太阳可以扩大到中心,进入主界面 16 | * 闪屏中的字体叫做 old English , 纽约时报专用字体 17 | 18 | ![splash](https://raw.githubusercontent.com/Werb/GankWithZhihu/master/screenshots/splash.png) 19 | 20 | ### 主界面 21 | * 汇集了知乎日报,干货集中营,以及好奇心日报,三种不同风格的阅读体验 22 | * 知乎日报 API 取自[ZhihuDailyPurify](https://github.com/izzyleung/ZhihuDailyPurify/wiki/%E7%9F%A5%E4%B9%8E%E6%97%A5%E6%8A%A5-API-%E5%88%86%E6%9E%90) 23 | * 干货集中营 API 取自[gank.io](http://gank.io/api) 24 | * 好奇心日报 API 是自己爬取所得,后续会整理到 GitHub 上 25 | 26 | ![main](https://raw.githubusercontent.com/Werb/GankWithZhihu/master/screenshots/zhigan.png) 27 | 28 | ### 一些有意思的地方 29 | * 知乎日报的详细详细界面,不是采用 webView 加载 url路径实现的,而是根据 api 返回的 html 标签代码,拼接 Css 和 JS 实现的,很有意思,我分析知乎这样做的原因,应该是为了实现在无网状态下,同样可以保持阅读体验 30 | * 各种APi返回的数据格式都很复杂,特别是好奇心日报,不单单是list集合,所以在项目中,使用 RecyclerView 加载了很多复杂布局,特别是实现了如何根据数据来判断布局的加载 31 | * 从开始学习到第一次使用 MVP + RxJava + Retrofit 开发项目,真正体会到了它的方便与强大之处 32 | * 项目中,有很多代码是可以重复利用的,为了更好的体会 MVP 思想,我目前还没有重构,后续会根据功能进行优化 33 | * 目前对内存优化问题,我控制的不是很好,如果有在这方面有经验很擅长的同学,希望可以联系我 34 | 35 | ![detail](https://raw.githubusercontent.com/Werb/GankWithZhihu/master/screenshots/maindetail.png) 36 | 37 | ### 很高兴你看到这里 38 | 39 | > 有时候啊 你总是在追赶前面的人 40 | 41 | > 总是抱怨自己为什么不能再努力一点 42 | 43 | >累了你可以停下来 看看原来的自己 44 | 45 | >其实你已经很了不起了。 46 | 47 | ![aboutme](https://raw.githubusercontent.com/Werb/GankWithZhihu/master/screenshots/aboutme.png) 48 | 49 | * [业余时间写了一个第三方微博(不使用官方SDK)](https://github.com/Werb/Werb) 50 | * 欢迎 Star 和 Fork 51 | 52 | 53 | ### License 54 | * 感谢开源项目 [ZhihuDailyPurify](https://github.com/izzyleung/ZhihuDailyPurify/wiki/%E7%9F%A5%E4%B9%8E%E6%97%A5%E6%8A%A5-API-%E5%88%86%E6%9E%90) 55 | * 感谢 [gank.io](http://gank.io/api) 56 | * 感谢 [MeiZhi](https://github.com/drakeet/Meizhi) 57 | * 同时希望可以帮助到其他人 58 | * 项目中用到的 api 所有权归 知乎,gank.io,好奇心日报所有,本项目仅是用来学习使用 59 | 60 | 61 | 62 | 63 | 64 | Copyright 2016 Werb 65 | 66 | Licensed under the Apache License, Version 2.0 (the "License"); 67 | you may not use this file except in compliance with the License. 68 | You may obtain a copy of the License at 69 | 70 | http://www.apache.org/licenses/LICENSE-2.0 71 | 72 | Unless required by applicable law or agreed to in writing, software 73 | distributed under the License is distributed on an "AS IS" BASIS, 74 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 75 | See the License for the specific language governing permissions and 76 | limitations under the License. 77 | 78 | 79 | 80 | 81 | 82 | ### Contact Me 83 | * Email: 1025004680@qq.com 84 | * Blog : [Werb's blog](http://werb.github.io/) 85 | * Weibo: [UMR80](http://weibo.com/singerwannber ) 86 | * GitHub: [Werb](https://github.com/Werb) 87 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | 6 | defaultConfig { 7 | applicationId "com.werb.gankwithzhihu" 8 | minSdkVersion 19 9 | targetSdkVersion 27 10 | versionCode 1 11 | versionName "1.0" 12 | javaCompileOptions { 13 | annotationProcessorOptions { 14 | includeCompileClasspath = true 15 | } 16 | } 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(include: ['*.jar'], dir: 'libs') 33 | implementation 'junit:junit:4.12' 34 | implementation 'com.android.support:appcompat-v7:27.1.1' 35 | implementation 'io.reactivex:rxandroid:1.2.1' 36 | implementation 'com.squareup.retrofit2:retrofit:2.1.0' 37 | implementation 'com.squareup.retrofit2:converter-gson:2.1.0' 38 | implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0' 39 | implementation 'com.android.support:design:27.1.1' 40 | implementation 'com.android.support:recyclerview-v7:27.1.1' 41 | implementation 'com.android.support:cardview-v7:27.1.1' 42 | implementation 'com.jakewharton:butterknife:7.0.1' 43 | implementation 'com.github.bumptech.glide:glide:3.7.0' 44 | implementation 'com.github.chrisbanes.photoview:library:1.2.3' 45 | implementation 'com.github.tbruyelle:rxpermissions:0.10.2' 46 | } 47 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\CodeTools\Android\android_sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/werb/gankwithzhihu/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 19 | 20 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 31 | 32 | 33 | 35 | 36 | 37 | 39 | 40 | 41 | 43 | 44 | 45 | 47 | 48 | 49 | 51 | 52 | 53 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /app/src/main/assets/fonts/dqht.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/werbhelius/GankWithZhihu/47181b9c309c75150c3484293294a447b6f9bb74/app/src/main/assets/fonts/dqht.ttf -------------------------------------------------------------------------------- /app/src/main/assets/fonts/rm_albion.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/werbhelius/GankWithZhihu/47181b9c309c75150c3484293294a447b6f9bb74/app/src/main/assets/fonts/rm_albion.ttf -------------------------------------------------------------------------------- /app/src/main/java/com/werb/gankwithzhihu/MyApp.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | /** 7 | * Created by Werb on 2016/8/18. 8 | * Werb is Wanbo. 9 | * Contact Me : werbhelius@gmail.com 10 | */ 11 | public class MyApp extends Application { 12 | 13 | private static final String DB_NAME = "weibo.db"; 14 | 15 | public static Context mContext; 16 | 17 | @Override 18 | public void onCreate() { 19 | super.onCreate(); 20 | mContext = getApplicationContext(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/werb/gankwithzhihu/api/ApiService.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu.api; 2 | 3 | import com.werb.gankwithzhihu.api.services.DailyApi; 4 | import com.werb.gankwithzhihu.api.services.GankApi; 5 | import com.werb.gankwithzhihu.api.services.ZhihuApi; 6 | 7 | import retrofit2.Retrofit; 8 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; 9 | import retrofit2.converter.gson.GsonConverterFactory; 10 | 11 | import static com.werb.gankwithzhihu.api.Constant.DAILY_BASE_URL; 12 | import static com.werb.gankwithzhihu.api.Constant.GANK_BASE_URL; 13 | import static com.werb.gankwithzhihu.api.Constant.ZHIHU_BASE_URL; 14 | 15 | /** 16 | * Created by Werb on 2016/8/18. 17 | * Werb is Wanbo. 18 | * Contact Me : werbhelius@gmail.com 19 | * Singleton Factory with retrofit 20 | */ 21 | public class ApiService { 22 | 23 | private static ZhihuApi zhihuApiSingleton = null; 24 | private static GankApi gankApiSingleton = null; 25 | private static DailyApi dailyApiSingleton = null; 26 | 27 | //return Singleton 28 | public static ZhihuApi getZhihuApiSingleton() { 29 | if (zhihuApiSingleton == null) { 30 | synchronized (ZhihuApi.class){ 31 | if (zhihuApiSingleton == null){ 32 | Retrofit retrofit_zhihu = new Retrofit.Builder() 33 | .baseUrl(ZHIHU_BASE_URL) 34 | .client(OkHttpManager.getInstance()) 35 | .addConverterFactory(GsonConverterFactory.create()) 36 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 37 | .build(); 38 | zhihuApiSingleton = retrofit_zhihu.create(ZhihuApi.class); 39 | } 40 | } 41 | } 42 | return zhihuApiSingleton; 43 | } 44 | 45 | public static GankApi getGankApiSingleton() { 46 | if (gankApiSingleton == null) { 47 | synchronized (GankApi.class){ 48 | if (gankApiSingleton == null){ 49 | Retrofit retrofit_gank = new Retrofit.Builder() 50 | .baseUrl(GANK_BASE_URL) 51 | .client(OkHttpManager.getInstance()) 52 | .addConverterFactory(GsonConverterFactory.create()) 53 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 54 | .build(); 55 | gankApiSingleton = retrofit_gank.create(GankApi.class); 56 | } 57 | } 58 | } 59 | return gankApiSingleton; 60 | } 61 | 62 | public static DailyApi getDailyApiSingleton() { 63 | if (dailyApiSingleton == null) { 64 | synchronized (DailyApi.class){ 65 | if (dailyApiSingleton == null){ 66 | Retrofit retrofit_daily= new Retrofit.Builder() 67 | .baseUrl(DAILY_BASE_URL) 68 | .client(OkHttpManager.getInstance()) 69 | .addConverterFactory(GsonConverterFactory.create()) 70 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 71 | .build(); 72 | dailyApiSingleton = retrofit_daily.create(DailyApi.class); 73 | } 74 | } 75 | } 76 | return dailyApiSingleton; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/werb/gankwithzhihu/api/Constant.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu.api; 2 | 3 | public class Constant { 4 | public static final String ZHIHU_BASE_URL = "http://news-at.zhihu.com/api/4/"; 5 | public static final String GANK_BASE_URL = "http://gank.io/api/"; 6 | public static final String DAILY_BASE_URL = "http://app3.qdaily.com/app3/"; 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/werb/gankwithzhihu/api/OkHttpManager.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu.api; 2 | 3 | import com.werb.gankwithzhihu.MyApp; 4 | import com.werb.gankwithzhihu.util.StateUtils; 5 | 6 | import java.io.File; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | import okhttp3.Cache; 10 | import okhttp3.CacheControl; 11 | import okhttp3.Interceptor; 12 | import okhttp3.OkHttpClient; 13 | import okhttp3.Request; 14 | import okhttp3.Response; 15 | 16 | public class OkHttpManager { 17 | 18 | private static OkHttpClient client = null; 19 | 20 | public static OkHttpClient getInstance() { 21 | if (client == null) { 22 | synchronized (OkHttpManager.class){ 23 | if (client == null) 24 | { 25 | //cache url 26 | File httpCacheDirectory = new File(MyApp.mContext.getCacheDir(), "responses"); 27 | int cacheSize = 10 * 1024 * 1024; // 10 MiB 28 | Cache cache = new Cache(httpCacheDirectory, cacheSize); 29 | 30 | Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = chain -> { 31 | 32 | CacheControl.Builder cacheBuilder = new CacheControl.Builder(); 33 | cacheBuilder.maxAge(0, TimeUnit.SECONDS); 34 | cacheBuilder.maxStale(365, TimeUnit.DAYS); 35 | CacheControl cacheControl = cacheBuilder.build(); 36 | 37 | Request request = chain.request(); 38 | if (!StateUtils.isNetworkAvailable(MyApp.mContext)) { 39 | request = request.newBuilder() 40 | .cacheControl(cacheControl) 41 | .build(); 42 | 43 | } 44 | Response originalResponse = chain.proceed(request); 45 | if (StateUtils.isNetworkAvailable(MyApp.mContext)) { 46 | int maxAge = 0; // read from cache 47 | return originalResponse.newBuilder() 48 | .removeHeader("Pragma") 49 | .header("Cache-Control", "public ,max-age=" + maxAge) 50 | .build(); 51 | } else { 52 | int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale 53 | return originalResponse.newBuilder() 54 | .removeHeader("Pragma") 55 | .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) 56 | .build(); 57 | } 58 | }; 59 | client = new OkHttpClient.Builder() 60 | .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) 61 | .cache(cache).build(); 62 | } 63 | } 64 | } 65 | return client; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/com/werb/gankwithzhihu/api/services/DailyApi.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu.api.services; 2 | 3 | import com.werb.gankwithzhihu.bean.daily.DailyTimeLine; 4 | 5 | import retrofit2.http.GET; 6 | import retrofit2.http.Path; 7 | import rx.Observable; 8 | 9 | /** 10 | * Created by Werb on 2016/9/2. 11 | * Werb is Wanbo. 12 | * Contact Me : werbhelius@gmail.com 13 | */ 14 | public interface DailyApi { 15 | 16 | @GET("homes/index/{num}.json") 17 | Observable getDailyTimeLine(@Path("num") String num); 18 | 19 | @GET("options/index/{id}/{num}.json") 20 | Observable getDailyFeedDetail(@Path("id") String id,@Path("num") String num); 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/werb/gankwithzhihu/api/services/GankApi.java: -------------------------------------------------------------------------------- 1 | package com.werb.gankwithzhihu.api.services; 2 | 3 | import com.werb.gankwithzhihu.bean.gank.GankData; 4 | import com.werb.gankwithzhihu.bean.gank.Meizhi; 5 | import com.werb.gankwithzhihu.bean.gank.Video; 6 | import retrofit2.http.GET; 7 | import retrofit2.http.Path; 8 | import rx.Observable; 9 | 10 | /** 11 | * Created by Werb on 2016/8/18. 12 | * Werb is Wanbo. 13 | * Contact Me : werbhelius@gmail.com 14 | * get Gank with retrofit 15 | */ 16 | public interface GankApi { 17 | 18 | @GET("data/福利/10/{page}") 19 | Observable getMeizhiData(@Path("page") int page); 20 | 21 | @GET("day/{year}/{month}/{day}") 22 | Observable getGankData(@Path("year") int year, @Path("month") int month, @Path("day") int day); 23 | 24 | @GET("data/休息视频/10/{page}") 25 | Observable