RxJava
& RxAndroid
compile 'io.reactivex:rxjava:1.1.3'
compile 'io.reactivex:rxandroid:1.1.0'
//如果结合Retrofit使用,需要添加以下依赖
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
private void simpleDemo() {
String[] arr = {"afdsa", "bfdsa", "cfda"};
Observable
.from(arr)
.filter(new Func1() {
@Override
public Boolean call(String s) {
return s.startsWith("a");
}
})
.map(new Func1() {
@Override
public String call(String s) {
return s + " from Alpha";
}
})
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("Rxjava:" + s.length());
}
});
for (int i = 0; i < arr.length; i++) {
String temp = arr[i];
if (temp.startsWith("a")) {
temp += " from Alpha";
System.out.println("Normal:" + temp.length());
}
}
private void simpleDemo() {
final int drawID = R.mipmap.ic_launcher;
Observable
.create(new Observable.OnSubscribe() {
@Override
public void call(Subscriber super Drawable> subscriber) {
Drawable drawable = getResources().getDrawable(drawID);
subscriber.onNext(drawable);
subscriber.onCompleted();
}
})
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
@Override
public void onNext(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
});
}
内置的Scheduler:
指定线程的方法:
private void simpleDemo() {
Observable
.just(R.mipmap.ic_launcher)
.map(new Func1() {
@Override
public Drawable call(Integer integer) {
return getResources().getDrawable(integer);
}
})
.subscribe(new Action1() {
@Override
public void call(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
});
}
public class Course {
private String name;
private int id;
public Course(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class Student {
private String name;
private ArrayList courses;
public Student(String name, ArrayList courses) {
this.name = name;
this.courses = courses;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList getCourses() {
return courses;
}
public void setCourses(ArrayList courses) {
this.courses = courses;
}
}
private void student() {
Course yuwen = new Course("语文", 1);
Course shuxue = new Course("数学", 2);
Course yingyu = new Course("英文", 3);
Course lishi = new Course("历史", 4);
Course zhengzhi = new Course("政治", 5);
Course xila = new Course("希腊语", 6);
ArrayList course1 = new ArrayList<>();
course1.add(yuwen);
course1.add(shuxue);
course1.add(yingyu);
course1.add(lishi);
course1.add(zhengzhi);
course1.add(xila);
Student zhangsan = new Student("zhangsan", course1);
Observable.just(zhangsan)
.flatMap(new Func1>() {
@Override
public Observable call(Student student) {
return Observable.from(student.getCourses());
}
}).subscribe(new Action1() {
@Override
public void call(Course course) {
System.out.println(course.getName());
}
});
}
添加依赖:
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
利用http://www.jsonschema2pojo.org/创建数据模型
创建REST API 接口.注意此时返回的不能是Call而是Observable.示例代码:
public interface LocationInterface {
// http://ip.taobao.com/service/getIpInfo.php?ip=202.178.10.23
@GET("/service/getIpInfo.php")
public Observable getLocation(@Query("ip") String ip);
}
创建Retrofit对象,发起请求.注意此时Retrofit需要添加addCallAdapterFactory.示例代码:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE2)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
LocationInterface locationInterface = retrofit.create(LocationInterface.class);
Observable location = locationInterface.getLocation("8.8.8.8");
location
.subscribeOn(Schedulers.io())
.map(new Func1() {
@Override
public String call(Location location) {
return location.getData().getCountry();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(String s) {
textView.setText(s);
}
});