• Home
  • Contact Us
  • About Us
  • Search
VTUFORUM
  • Home
  • Portal
  • Member List
  • QuestionPapers


To access all the content of this forum/community Please:

Login                       OR Register




VTUFORUM / KnowledgeBase / Concepts and Tutorials v
1 2 3 4 5 Next »
/ RxJava in Android example code

Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threaded Mode | Linear Mode
RxJava in Android example code
Author Message
Sandeep Offline
Sleepy Admin
*******

Posts: 731
Joined: Sep 2012
Reputation: 16
Post: #1
RxJava in Android example code
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/grokki...va-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
10-07-2015 10:41 PM
Visit this user's website Find all posts by this user Quote this message in a reply
« Next Oldest | Next Newest »
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Twitter MySpace

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Student Feedback system for College in PHP(SOurce code) Sandeep 19 32,838 08-30-2017 01:01 AM
Last Post: shashikantsngh
  Form validation made easy in Android with saripaar Sandeep 0 2,084 09-28-2015 10:03 AM
Last Post: Sandeep
  Develop Android Application using PhoneGap(Cordova) Sandeep 2 2,889 07-03-2015 01:56 PM
Last Post: Sandeep
  Run Android ADB commands from Java programs Sandeep 0 5,749 04-01-2015 07:16 PM
Last Post: Sandeep
  Dynamic Instrumentation of Android App using Monkeytalk Sandeep 0 1,555 03-27-2015 06:48 PM
Last Post: Sandeep
  Setup MonkeyTalk with Android Studio Sandeep 2 3,493 02-17-2015 09:30 AM
Last Post: Sandeep

  • View a Printable Version
  • Send this Thread to a Friend
  • Subscribe to this thread
Forum Jump:


User(s) browsing this thread: 1 Guest(s)
Contact Us | VTUFORUM | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication
Powered By MyBB, © 2002-2018 MyBB Group. Theme created by TopAdmin.Net
copyright © 2012 vtuforum.com