Retrofit网络框架搭建

来源:互联网 发布:那个程序员txt总攻大人 编辑:程序博客网 时间:2024/05/22 00:45
项目地址https://github.com/helloworld107/ZhNews
本项目运用了很多新鲜前沿的技术,mvp+rxjava+retrofit+5.0系统新特性布局,recycerview,项目结构不算复杂,对于学习新技术的朋友有很好的教学意义,下面开始撸代码,分析一下该项目的核心知识点部分
先看框架
大体上工具类,自定义控件,适配器,bean实体类与mvc并没有任何不同,由于使用的mvp+rxjava+retrofit,差异的地方在于api获取方式,和presenter,view接口,咱们先从简单的来,看看网络请求的类
这是栏目其中的一个api,如果按照retrofit的方式返回的应该是一个
Call<ResponseBody>.由于结合rxjava,所以返回一个被观察者对象observable,具体对应的实体类都是json解析很简单这里不多说,
通过这种写法就可以通过rxjava的方法调用了。

public interface DailyApi {

@GET("homes/index/{num}.json")
Observable<DailyTimeLine> getDailyTimeLine(@Path("num") String num);

@GET("options/index/{id}/{num}.json")
Observable<DailyTimeLine> getDailyFeedDetail(@Path("id") String id, @Path("num") String num);
}

之后写一个返回对应retrofit的类
public classApiRetrofit {

//三个基本网址
public static finalStringZHIHU_BASE_URL="http://news-at.zhihu.com/api/4/";
public static finalStringGANK_BASE_URL="http://gank.io/api/";
public static finalStringDAILY_BASE_URL="http://app3.qdaily.com/app3/";
//三个访问类
publicZhihuApiZhihuApiService;
publicGankApiGankApiService;
publicDailyApiDailyApiService;

publicZhihuApi getZhihuApiService() {
returnZhihuApiService;
}

publicGankApi getGankApiService() {
returnGankApiService;
}

publicDailyApi getDailyApiService() {
returnDailyApiService;
}

//饿汉模式,构造方法的时候就把类都创建好了
ApiRetrofit() {
//搞一个自己的缓存
File httpCacheDirectory =newFile(MyApp.mContext.getCacheDir(),"responses");
intcacheSize = 10*1024*1024;// 10 MiB
Cache cache =newCache(httpCacheDirectory,cacheSize);

OkHttpClient client=newOkHttpClient.Builder().addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR).
//这里有个拦截器,用来监测访问数据的信息以及更改一些参数,这里不是重点代码略过
cache(cache).build();

Retrofit retrofit_zhihu =newRetrofit.Builder()
.baseUrl(ZHIHU_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())结合rxjava的必要写法
.build();

Retrofit retrofit_gank =newRetrofit.Builder()
.baseUrl(GANK_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

Retrofit retrofit_daily=newRetrofit.Builder()
.baseUrl(DAILY_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

ZhihuApiService=retrofit_zhihu.create(ZhihuApi.class);
GankApiService=retrofit_gank.create(GankApi.class);
DailyApiService=retrofit_daily.create(DailyApi.class);


}
这样一来就可以在开发中调用了,不过作者又优化了一下,写了一个工厂类实习单例,同步锁
public classApiFactory {

//新的单例模式写法,靠一个静态类来位置单例
protected static final Object monitor=newObject();
staticZhihuApi zhihuApiSingleton=null;
staticGankApi gankApiSingleton=null;
staticDailyApi dailyApiSingleton=null;
//return Singleton
//return Singleton
public staticZhihuApi getZhihuApiSingleton() {
synchronized(monitor) {
if(zhihuApiSingleton==null) {
zhihuApiSingleton=newApiRetrofit().getZhihuApiService();
}
returnzhihuApiSingleton;
}
}

public staticGankApi getGankApiSingleton() {
synchronized(monitor) {
if(gankApiSingleton==null) {
gankApiSingleton=newApiRetrofit().getGankApiService();
}
returngankApiSingleton;
}
}

public staticDailyApi getDailyApiSingleton() {
synchronized(monitor) {
if(dailyApiSingleton==null) {
dailyApiSingleton=newApiRetrofit().getDailyApiService();
}
returndailyApiSingleton;
}
}
}

0 0
原创粉丝点击