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.toJson()时内存溢出StackOverflowError
- Android学习笔记45之gson解析json
- Android中gson、jsonobject解析JSON的方法详解
- JAVA使用Gson解析json数据实例解析
- Java中利用gson解析Json实例教程
- 利用gson将map转为json示例
- Android Retrofit 2.0框架上传图片解决方案
- 在struts2 中使用jQuery 的Ajax 技术
- 跟我一起看Retrofit 2.0的源码
- RxJava之subscribeOn解惑
- Gson的使用-android
- Rxjava
- http缓存
- Gson详解:Java对象与JSON相互转换的利器
- Gson工具类
- 走在互联网的大陆上:一、几款流行的JSON库效率分析
- android REST APIs 工具 Retrofit的应用
- android开发练习二-Retrofit
- Gson简介和入门
- Google Gson 使用简介