VTUFORUM
RxJava in Android example code - Printable Version

+- VTUFORUM (http://www.vtuforum.com)
+-- Forum: KnowledgeBase (/Forum-KnowledgeBase)
+--- Forum: Concepts and Tutorials (/Forum-Concepts-and-Tutorials)
+--- Thread: RxJava in Android example code (/Thread-RxJava-in-Android-example-code)



RxJava in Android example code - Sandeep - 10-07-2015 10:41 PM

Hello Androiders,

Thanks for visiting our community, hope you will get the info you are searching you. This is the article on how to use ReactiveX Java in Android. Its quite simple and saved lot of time. Here am showing a sample example of requesting the URL and fetching the response and displaying back on your App.

RxJava has a basic concept of Observer and Observable. As the name indicates, Observable emits the data or produces the data but observer consumer the data. When we bind them togeather we will get the required response. But what are the advantages??

It will basically used in complex applications where lot of request/reply actions are going on or you are consuming the data from more then one resources. In that case we have to do lot of reques/response to play with you will end up messing up with AsycnTasks and handlers. So RxJava is clean way of handling thus part.

Here in my scenario, I am calling a url which will replay a JSON data and loading it on a textView.

So we have to create 2 components. One which emits the data and one which consumes

So lets start with a emitter, since we know that Andorid won't allow the networking(time consuming) operation on the Main thread. we have to create the emitter on seperate thread. So don't worry you don't have to write a thread now. Emitter(Observable) has a option to handle this. See this code snippet you will understand it well

Quote:Observable<String> fetchFromGoogle = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
String data = fetchData();
subscriber.onNext(data); // Emit the contents of the URL
subscriber.onCompleted(); // Nothing more to emit
}catch(Exception e){
subscriber.onError(e); // In case there are network errors
}
}
});

fetchData() is some function, which will read data from a URL. Am using a okHttp method to read it. See this code, try to use okHttp. It all about reading a JSON

Quote:public String fetchData(){
String intialURL = "http://api.androidhive.info/volley/person_object.json";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(intialURL)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

Now create a subscriber(consumer) which consumes this data and display it. It not only conumse, it call the Observabale(emitter) and make it run in the differnt thread. See this code, you will understand it


Quote:txtObserve = (TextView) findViewById(R.id.txtObserver);
fetchFromGoogle
.subscribeOn(Schedulers.newThread()) // Create a new Thread
.observeOn(AndroidSchedulers.mainThread()) // Use the UI thread
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
txtObserve.setText(s); // Change a View
}
});


This will work fine according to me.

For more help on RxJava and its basics visit
http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/

Its the best I found till now.

Anyway check the code of this tutorial on my Github page
https://github.com/sandeeptengale/RxJavaAndroid

Happy coding guys Big Grin Big Grin