Skip to content

Commit

Permalink
Add RetrofitUrlManager
Browse files Browse the repository at this point in the history
  • Loading branch information
JessYanCoding committed Apr 24, 2018
1 parent 6d00aa6 commit 0d1a53a
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 275 deletions.
1 change: 1 addition & 0 deletions CommonSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ dependencies {
exclude module: 'support-v4'
exclude module: 'support-annotations'
}
api rootProject.ext.dependencies["retrofit-url-manager"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import me.jessyan.armscomponent.commonsdk.http.Api;
import me.jessyan.armscomponent.commonsdk.http.SSLSocketClient;
import me.jessyan.armscomponent.commonsdk.imgaEngine.Strategy.CommonGlideImageLoaderStrategy;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import okhttp3.OkHttpClient;
import timber.log.Timber;

Expand Down Expand Up @@ -68,6 +69,8 @@ public void applyOptions(Context context, GlobalConfigModule.Builder builder) {
public void configOkhttp(Context context, OkHttpClient.Builder builder) {
builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getTrustManager());
builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier());
//让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl. 详细使用请方法查看 https://github.com/JessYanCoding/RetrofitUrlManager
RetrofitUrlManager.getInstance().with(builder);
}
})
.rxCacheConfiguration((context1, rxCacheBuilder) -> {//这里可以自己自定义配置RxCache的参数
Expand All @@ -92,7 +95,7 @@ public void onCreate(Application application) {
ButterKnife.setDebug(true);
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)

RetrofitUrlManager.getInstance().setDebug(true);
}
ARouter.init(application); // 尽可能早,推荐在Application中初始化
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AppLifecyclesImpl implements AppLifecycles {

@Override
public void attachBaseContext(Context base) {
// MultiDex.install(base); //这里比 onCreate 先执行,常用于 MultiDex 初始化,插件化框架的初始化

}

@Override
Expand All @@ -48,8 +48,11 @@ public void onCreate(Application application) {
// You should not init your app in this process.
return;
}
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import java.util.List;

import me.jessyan.armscomponent.gank.BuildConfig;

/**
* ================================================
* App 的全局配置信息在此配置, 需要将此实现类声明到 AndroidManifest 中
Expand Down Expand Up @@ -62,16 +64,19 @@ public void injectActivityLifecycle(Context context, List<Application.ActivityLi

@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AppLifecyclesImpl implements AppLifecycles {

@Override
public void attachBaseContext(Context base) {
// MultiDex.install(base); //这里比 onCreate 先执行,常用于 MultiDex 初始化,插件化框架的初始化

}

@Override
Expand All @@ -48,8 +48,11 @@ public void onCreate(Application application) {
// You should not init your app in this process.
return;
}
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import java.util.List;

import me.jessyan.armscomponent.gold.BuildConfig;

/**
* ================================================
* App 的全局配置信息在此配置, 需要将此实现类声明到 AndroidManifest 中
Expand Down Expand Up @@ -62,16 +64,19 @@ public void injectActivityLifecycle(Context context, List<Application.ActivityLi

@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import com.squareup.leakcanary.RefWatcher;

import me.jessyan.armscomponent.zhihu.BuildConfig;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

import static me.jessyan.armscomponent.zhihu.mvp.model.api.Api.ZHIHU_DOMAIN;
import static me.jessyan.armscomponent.zhihu.mvp.model.api.Api.ZHIHU_DOMAIN_NAME;

/**
* ================================================
Expand All @@ -38,7 +42,7 @@ public class AppLifecyclesImpl implements AppLifecycles {

@Override
public void attachBaseContext(Context base) {
// MultiDex.install(base); //这里比 onCreate 先执行,常用于 MultiDex 初始化,插件化框架的初始化

}

@Override
Expand All @@ -48,8 +52,12 @@ public void onCreate(Application application) {
// You should not init your app in this process.
return;
}
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
RetrofitUrlManager.getInstance().putDomain(ZHIHU_DOMAIN_NAME, ZHIHU_DOMAIN);
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import java.util.List;

import me.jessyan.armscomponent.zhihu.BuildConfig;

/**
* ================================================
* App 的全局配置信息在此配置, 需要将此实现类声明到 AndroidManifest 中
Expand Down Expand Up @@ -62,16 +64,19 @@ public void injectActivityLifecycle(Context context, List<Application.ActivityLi

@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import io.reactivex.Observable;
import me.jessyan.armscomponent.zhihu.mvp.contract.UserContract;
import me.jessyan.armscomponent.zhihu.mvp.model.api.service.UserService;
import me.jessyan.armscomponent.zhihu.mvp.model.api.service.ZhihuService;
import me.jessyan.armscomponent.zhihu.mvp.model.entity.User;
import timber.log.Timber;

Expand All @@ -55,7 +55,7 @@ public UserModel(IRepositoryManager repositoryManager) {
public Observable<List<User>> getUsers(int lastIdQueried, boolean update) {
//使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
return mRepositoryManager
.obtainRetrofitService(UserService.class)
.obtainRetrofitService(ZhihuService.class)
.getUsers(lastIdQueried, USERS_PER_PAGE);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
* ================================================
*/
public interface Api {
String APP_DOMAIN = "https://api.github.com";
String RequestSuccess = "0";
String ZHIHU_DOMAIN_NAME = "zhihu";
String ZHIHU_DOMAIN = "http://news-at.zhihu.com";
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,37 @@
import java.util.List;

import io.reactivex.Observable;
import me.jessyan.armscomponent.zhihu.mvp.model.entity.User;
import me.jessyan.armscomponent.zhihu.mvp.model.entity.DailyListBean;
import me.jessyan.armscomponent.zhihu.mvp.model.entity.User;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Query;

import static me.jessyan.armscomponent.zhihu.mvp.model.api.Api.ZHIHU_DOMAIN_NAME;
import static me.jessyan.retrofiturlmanager.RetrofitUrlManager.DOMAIN_NAME_HEADER;

/**
* ================================================
* 展示 {@link Retrofit#create(Class)} 中需要传入的 ApiService 的使用方式
* 存放关于用户的一些 API
* 存放关于 zhihu 的一些 API
* <p>
* Created by JessYan on 08/05/2016 12:05
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public interface UserService {
public interface ZhihuService {
String HEADER_API_VERSION = "Accept: application/vnd.github.v3+json";

@Headers({HEADER_API_VERSION})
@GET("/users")
Observable<List<User>> getUsers(@Query("since") int lastIdQueried, @Query("per_page") int perPage);

/**
* 最新日报
*/
@Headers({DOMAIN_NAME_HEADER + ZHIHU_DOMAIN_NAME})
@GET("/api/4/news/latest")
Observable<DailyListBean> getDailyList();
}
Loading

0 comments on commit 0d1a53a

Please sign in to comment.