您的位置:首页 > 编程语言 > Java开发

Retrofit+Rxjava

2016-04-18 20:29 686 查看
1,在清单文件里面配置
compile 'com.google.code.gson:gson:2.3'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
provided 'org.glassfish:javax.annotation:10.0-b28'
2,定义一个Service类
public class CallService {public static final HttpService service;static {Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG).registerTypeAdapter(Date.class, new DateDeserializer()).setDateFormat(DateFormat.LONG).create();Retrofit retrofit = new Retrofit.Builder().baseUrl(Consts.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson))/*** 设置回调库,采用Rxjava*/.addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();service = retrofit.create(HttpService.class);}}
3,观察者模式CallService.service.listPlayGuideByObservable(i++, MapUtils.getValueMap(playGuide))//被观察者执行io接口.subscribeOn(Schedulers.io())/*被观察者的回调函数。对于数据再次封装。在时间转换的时候用.doOnNext(new Action1<Page<PlayGuide>>() {@Overridepublic void call(Page<PlayGuide> playGuidePage) {int j = 0;for (PlayGuide playGuide : playGuidePage.getContent()) {Log.d(TAG, "playGuide" + j++);//Dao.save(playGuide);}}})*///观察者在主线程运行.observeOn(AndroidSchedulers.mainThread())//执行观察者.subscribe(new Observer<Page<PlayGuide>>() {@Overridepublic void onCompleted() {initAdapter();stopRefresh();}@Overridepublic void onError(Throwable e) {}@Overridepublic void onNext(Page<PlayGuide> playGuidePage) {playGuides = playGuidePage.getContent();playGuides.addAll(playGuides);}});}4,定义一个
HttpService接口。
 @GET("/bike/playGuide/listPlayGuide?sort=baiduPage,Asc&sort=id,Asc")Observable<Page<PlayGuide>> listPlayGuideByObservable(@Query("page") Integer page, @QueryMap Map<String, Object> playGuideMap);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gson rxjava retrofit