}
}
checkProvider(provider);
Gson gson = new GsonBuilder()
.setDateFormat(“yyyy-MM-dd HH:mm:ss”)
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(getClient(baseUrl, provider))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit = builder.build();
retrofitMap.put(baseUrl, retrofit);
providerMap.put(baseUrl, provider);
return retrofit;
}
private boolean empty(String baseUrl) {
return baseUrl == null || baseUrl.isEmpty();
}
private OkHttpClient getClient(String baseUrl, NetProvider provider) {
if (empty(baseUrl)) {
throw new IllegalStateException(“baseUrl can not be null”);
}
if (clientMap.get(baseUrl) != null) {
return clientMap.get(baseUrl);
}
checkProvider(provider);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(provider.configConnectTimeoutSecs() != 0
? provider.configConnectTimeoutSecs()
: connectTimeoutMills, TimeUnit.SECONDS);
builder.readTimeout(provider.configReadTimeoutSecs() != 0
? provider.configReadTimeoutSecs() : readTimeoutMills, TimeUnit.SECONDS);
builder.writeTimeout(provider.configWriteTimeoutSecs() != 0
? provider.configReadTimeoutSecs() : readTimeoutMills, TimeUnit.SECONDS);
CookieJar cookieJar = provider.configCookie();
if (cookieJar != null) {
builder.cookieJar(cookieJar);
}
provider.configHttps(builder);
RequestHandler handler = provider.configHandler();
if (handler != null) {
builder.addInterceptor(new NetInterceptor(handler));
}
Interceptor[] interceptors = provider.configInterceptors();
if (!empty(interceptors)) {
for (Interceptor interceptor : interceptors) {
builder.addInterceptor(interceptor);
}
}
if (provider.configLogEnable()) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
OkHttpClient client = builder.build();
clientMap.put(baseUrl, client);
providerMap.put(baseUrl, provider);
return client;
}
private boolean empty(Interceptor[] interceptors) {
return interceptors == null || interceptors.length == 0;
}
private void checkProvider(NetProvider provider) {
if (provider == null) {
throw new IllegalStateException(“must register provider first”);
}
}
public Map<String, Retrofit> getRetrofitMap() {
return retrofitMap;
}
public Map<String, OkHttpClient> getClientMap() {
return clientMap;
}
}
- 2、实现NetProvider接口并注入
NetMgr就是一个Retrofit的实现类,然后NetProvider是一个接口,需要在外部去实现,然后注入。再看一下NetProvider的实现类BaseNetProvider
public class BaseNetProvider implements NetProvider {
private static final long CONNECT_TIME_OUT = 30;
private static final long READ_TIME_OUT = 180;
private static final long WRITE_TIME_OUT = 30;
@Override
public Interceptor[] configInterceptors() {
return null;
}
@Override
public void configHttps(OkHttpClient.Builder builder) {
}
@Override
public CookieJar configCookie() {
return null;
}
@Override
public RequestHandler configHandler() {
return new HeaderHandler();
}
@Override
public long configConnectTimeoutSecs() {
return CONNECT_TIME_OUT;
}
@Override
public long configReadTimeoutSecs() {
return READ_TIME_OUT;
}
@Override
public long configWriteTimeoutSecs() {
return WRITE_TIME_OUT;
}
@Override
public boolean configLogEnable() {
return BuildConfig.DEBUG;
}
private class HeaderHandler implements RequestHandler {
@Override
public Request onBeforeRequest(Request request, Interceptor.Chain chain) {
return chain.request().newBuilder()
.addHeader(“X-Auth-Token”, Constant.accessToken)
.addHeader(“Authorization”, “”)
.build();
}
@Override
public Response onAfterRequest(Response response, Interceptor.Chain chain)
throws IOException {
ApiException e = null;
if (401 == response.code()) {
throw new ApiException(“登录已过期,请重新登录!”);
} else if (403 == response.code()) {
throw new ApiException(“禁止访问!”);
} else if (404 == response.code()) {
throw new ApiException(“链接错误”);
} else if (503 == response.code()) {
throw new ApiException(“服务器升级中!”);
} else if (500 == response.code()) {
throw new ApiException(“服务器内部错误!”);
}
return response;
}
}
在BaseNetProvider中实现了连接、读、写超时的时间处理,与请求和返回数据的请求头部处理。然后需要在Application中去注入BaseNetProvider
NetMgr.getInstance().registerProvider(new BaseNetProvider());
- 3、Observable实现
首先实现一个UseCase的基类,处理公共的使用方法。通过调用NetMgr.getInstance().getRetrofit(BuildConfig.BaseUrl).create(getType())来获取ApiService的实例,然后提供了指定线程的基类方法。至于PagingReq是一个分页模型,方便分页接口的使用。
public abstract class UseCase {
//用于分页请求
protected PagingReq pagingReq = new PagingReq();
protected T ApiClient() {
return NetMgr.getInstance().getRetrofit(BuildConfig.BaseUrl).create(getType());
}
//指定观察者与被观察者线程
protected Observable.Transformer<T, T> normalSchedulers() {
return new Observable.Transformer<T, T>() {
@Override
public Observable call(Observable source) {
return source.onTerminateDetach().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
}
private Class getType() {
Class entityClass = null;
Type t = getClass().getGenericSuperclass();
Type[] p = ((ParameterizedType) t).getActualTypeArguments();
entityClass = (Class) p[0];
return entityClass;
}
}
此处实现一个简单的获取城市信息的接口。 首先定义接口ApiService,然后实现获取Observable的方法
public class GetCitiesCase extends UseCase<GetCitiesCase.Api> {
interface Api {
@GET(“api/china/”)
Observable<List> getCitiesCase();
}
结语
- 现在随着短视频,抖音,快手的流行NDK模块开发也显得越发重要,需要这块人才的企业也越来越多,随之学习这块的人也变多了,音视频的开发,往往是比较难的,而这个比较难的技术就是NDK里面的技术。
- 音视频/高清大图片/人工智能/直播/抖音等等这年与用户最紧密,与我们生活最相关的技术一直都在寻找最终的技术落地平台,以前是windows系统,而现在则是移动系统了,移动系统中又是以Android占比绝大部分为前提,所以AndroidNDK技术已经是我们必备技能了。
- 要学习好NDK,其中的关于C/C++,jni,Linux基础都是需要学习的,除此之外,音视频的编解码技术,流媒体协议,ffmpeg这些都是音视频开发必备技能,而且
- OpenCV/OpenGl/这些又是图像处理必备知识,下面这些我都是当年自己搜集的资料和做的一些图,因为当年我就感觉视频这块会是一个大的趋势。所以提前做了一些准备。现在拿出来分享给大家。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
己搜集的资料和做的一些图,因为当年我就感觉视频这块会是一个大的趋势。所以提前做了一些准备。现在拿出来分享给大家。
[外链图片转存中…(img-2czyqyAW-1715593427214)]
[外链图片转存中…(img-G4uM01xs-1715593427218)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!