因为在项目中经常接触到RxJava和RxAndroid结合一起使用的项目,所以学习这些技术也是迫在眉睫的。
我也是这段时间开始着手去写,之前的时间都在了解理论上的东西。所以决定挽起袖子直接撸代码。
RxJava的基础理论建议去搜周伯通的Rxjava,讲解的非常详细(必看),本人也很感想他的敬业的精神。
1.简单的使用Rxjava,废话不多说,直接上代码
创建观察者:
/** * 观察者 */ Observerobserver = new Observer () { @Override public void onCompleted() { Log.e("TAG", "oncompleted()"); } @Override public void onError(Throwable e) { Log.e("TAG", "onError()"); } @Override public void onNext(String s) { Log.e("TAG", "onNext()" + s); } };
或者创建观察者的实现类:Subscriber
/** * 观察者(观察者的实现类) */ Subscribersubscriber = new Subscriber () { @Override public void onCompleted() { Log.e("TAG", "oncompleted()"); } @Override public void onError(Throwable e) { Log.e("TAG", "onError()"); } @Override public void onNext(String s) { Log.e("TAG", "onNext()" + s); } };
可以说,两者的效果是一样的。
接着创建可观察者(被观察者)Observable
/** * 可观察者(被观察者) */ Observable observale = Observable.create(new Observable.OnSubscribe() { @Override public void call(Subscriber super String> subscriber) { subscriber.onNext("Hello"); subscriber.onNext("My name is Avater!"); subscriber.onCompleted(); } });
好了,到此已经创建完毕,接着在onCreate方法中进行简单的调用:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
observale.subscribeOn(Schedulers.io()) //订阅在io线程(非主线程),不会阻塞主线程 .observeOn(AndroidSchedulers.mainThread()) //在主线程中观察 .subscribe(observer); //进行订阅关系}
Log:
03-15 12:06:45.837 2952-2952/com.avater.myapplication E/TAG: onNext()Hello
03-15 12:06:45.847 2952-2952/com.avater.myapplication E/TAG: onNext()My name is Avater!
03-15 12:06:45.847 2952-2952/com.avater.myapplication E/TAG: oncompleted()
是不是很快?是不是很懵逼?哈哈,这就对了,毕竟入门嘛,多实战,多理解!
下面附上一个使用Rxjava下载图片的例子:
private ImageView imageView; private String url = "https://ss0.bdstatic. com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
/** * 图片观察者 */ ObserverbitmapOberver = new Observer () { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Toast.makeText(MainActivity.this, "图片下载失败", Toast.LENGTH_SHORT).show(); } @Override public void onNext(Bitmap bitmap) { imageView.setImageBitmap(bitmap); } };
/** * 可观察者(被观察者) */ ObservablebitmapObservable = Observable.create(new Observable.OnSubscribe () { @Override public void call(Subscriber super Bitmap> subscriber) { URL net; HttpURLConnection conn = null; InputStream inputStream = null; Bitmap bitmap = null; try { net = new URL(url); conn = (HttpURLConnection) net.openConnection(); inputStream = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { conn.disconnect(); try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } subscriber.onNext(bitmap); } });
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageview); bitmapObservable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(bitmapOberver); }
OK,装逼完毕....