Retrofit+RxJava+lambda使用示例
来源:互联网 发布:展翅高飞软件下载 编辑:程序博客网 时间:2024/04/29 23:08
Retrofit+RxJava+lambda使用示例
废话不说先上Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' classpath 'me.tatarka:gradle-retrolambda:2.4.+' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}
repositories { mavenCentral() maven { url "http://repo.maven.apache.org/maven2" }}apply plugin: 'com.android.application'apply plugin: 'retrolambda'android { compileSdkVersion 23 buildToolsVersion "23.0.2" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } retrolambda { System.getenv("JAVA8_HOME") System.getenv("JAVA7_HOME") javaVersion JavaVersion.VERSION_1_7 } defaultConfig { applicationId "com.as.testkaixin" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2' compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.jakewharton:butterknife:7.0.1'}
2 导入包后首先是Retrofit同步模式访问网络
public static final String API_URL = "https://api.github.com";public static class Contributor { public final String login; public final int contributions; public Contributor(int contributions, String login) { this.contributions = contributions; this.login = login; }}public interface GitHub{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> contributors (@Path("owner") String owner, @Path("repo") String repo);}public static void main(String... args) throws IOException { Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()).build(); GitHub gitHub = retrofit.create(GitHub.class); Call<List<Contributor>> call = gitHub.contributors("square", "retrofit"); List<Contributor> contributors = call.execute().body(); for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); }}
3 其次是Retrofit异步模式访问网络
public static final String API_URL = "https://api.github.com";public static class Contributor { public final String login; public final int contributions; public Contributor(int contributions, String login) { this.contributions = contributions; this.login = login; }}public interface GitHub{ @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> contributors (@Path("owner") String owner, @Path("repo") String repo);}public static void main(String... args) throws IOException { Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()).build(); GitHub gitHub = retrofit.create(GitHub.class); Call<List<Contributor>> call = gitHub.contributors("square", "retrofit"); call.enqueue(new Callback<List<Contributor>>() { @Override public void onResponse(Response<List<Contributor>> response, Retrofit retrofit) { List<Contributor> contributors =response.body(); for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } } @Override public void onFailure(Throwable t) { System.out.println("erro"); } });}
4 最后是
Retrofit+RxJava+lambda
public static final String API_URL = "https://api.github.com";public static class Contributor { public final String login; public final int contributions; public Contributor(int contributions, String login) { this.contributions = contributions; this.login = login; }}public interface GitHub{ @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> contributors (@Path("owner") String owner, @Path("repo") String repo);}public static void main(String... args) throws IOException { Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); GitHub gitHub = retrofit.create(GitHub.class); Observable<List<Contributor>> observable = gitHub.contributors("square", "retrofit"); observable.subscribe(new Action1<List<Contributor>>() { @Override public void call(List<Contributor> contributors) { for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } } }); System.out.println("================4lambda======================================"); observable.subscribe(contributors -> { for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } }); System.out.println("================4lambda from======================================"); observable.subscribe(contributors -> { Observable.from(contributors) .subscribe(contributor -> System.out.println(contributor.login + " (" + contributor.contributions + ")")); }); System.out.println("================4lambda flat Map======================================"); observable.flatMap(contributors -> Observable.from(contributors)) .subscribe(contributor -> System.out.println(contributor.login + " (" + contributor.contributions + ")")); System.out.println("================4lambda filter======================================"); observable.flatMap(contributors -> Observable.from(contributors)) .filter(contributor -> contributor != null) .subscribe(contributor -> System.out.println(contributor.login + " (" + contributor.contributions + ")")); System.out.println("================4lambda get======================================"); observable.flatMap(contributors -> Observable.from(contributors)) .take(5) .subscribe(contributor -> System.out.println(contributor.login + " (" + contributor.contributions + ")")); System.out.println("================4lambda doOnNext======================================"); observable.flatMap(contributors -> Observable.from(contributors)) .doOnNext(contributor -> System.out.println("do someThing...."+contributor.getClass().toString()) ) .subscribe(contributor ->System.out.println(contributor.login + " (" + contributor.contributions + ")"));}
0 0
- Retrofit+RxJava+lambda使用示例
- Rxjava+Retrofit 简单使用
- Retrofit+RxJava的使用
- 使用 RxJava 和 Retrofit
- RxJava + Retrofit 结合使用
- Retrofit+Rxjava简单使用
- retrofit与rxjava使用
- Retrofit+RXJava的使用
- Retrofit结合rxjava使用
- MVP+Retrofit+RxJava 使用
- Kotlin新语言使用RxJava+Retrofit请求网络数据+lambda表达式+RecyclerView展示+Glide展示图片
- 简单的mvp+retrofit+rxjava示例
- RxJava 和 Retrofit的调用示例
- 简单的mvp+retrofit+rxjava示例
- Retrofit 和 RxJava 结合使用
- 使用RxJava优化Retrofit请求
- RxJava,Retrofit,OkHttp3结合使用
- MVP+Retrofit+RxJava组合使用
- 参数编码规范
- js通过classname来获取元素
- (转)java序列化时候序列ID作用(结合实际场景)
- 一个菜鸟程序员的规划
- Android开发第二步 - 基类
- Retrofit+RxJava+lambda使用示例
- 自己的笔记___摘自别人(Android数据库中查找一条数据 query方法详解)
- 常见40个常用的js页面效果图
- 如何调用别人提供的接口
- sense2vec - a fast and accurate method for word sense disambiguation in neural word embeddings.
- Python适合大数据量的处理吗?
- EL表达式简介
- Android后台服务-service
- LeetCode 299 Bulls and Cows