. f Android Retrofit With RX Java Sample | Android Retrofit Example ~ Android Developers Blog

Saturday, 27 May 2017

Android Retrofit With RX Java Sample | Android Retrofit Example

Hello Friends,

This is an simple example of How we use Android Retrofit With RX .  I hope this
will helps you to start using Retrofit and  RX together.



1. MainActivity.java

  1. package android.com.retrofitwithrxsample;  
  2.   
  3. import android.com.retrofitwithrxsample.adapter.DataAdapter;  
  4. import android.com.retrofitwithrxsample.model.Country;  
  5. import android.com.retrofitwithrxsample.model.Result;  
  6. import android.com.retrofitwithrxsample.network.RequestInterface;  
  7. import android.os.Bundle;  
  8. import android.support.v7.app.AppCompatActivity;  
  9. import android.support.v7.widget.LinearLayoutManager;  
  10. import android.support.v7.widget.RecyclerView;  
  11. import android.widget.Toast;  
  12.   
  13. import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;  
  14.   
  15. import java.util.ArrayList;  
  16.   
  17. import io.reactivex.android.schedulers.AndroidSchedulers;  
  18. import io.reactivex.disposables.CompositeDisposable;  
  19. import io.reactivex.schedulers.Schedulers;  
  20. import okhttp3.OkHttpClient;  
  21. import okhttp3.logging.HttpLoggingInterceptor;  
  22. import retrofit2.Retrofit;  
  23. import retrofit2.converter.gson.GsonConverterFactory;  
  24. import com.newrelic.agent.android.NewRelic;  
  25.   
  26. /** 
  27.  * Create by: Mukesh Yadav 
  28.  * www.androiddevelopersolutions.com 
  29.  */  
  30. public class MainActivity extends AppCompatActivity {  
  31.   
  32.     private static String BASE_URL =  "http://services.groupkt.com/";  
  33.     private RecyclerView mRecyclerView;  
  34.   
  35.     private CompositeDisposable mCompositeDisposable;  
  36.   
  37.     private DataAdapter mAdapter;  
  38.   
  39.     private ArrayList<result> mAndroidArrayList;  
  40.     @Override  
  41.     protected void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.activity_main);  
  44.         mCompositeDisposable = new CompositeDisposable();  
  45.         initRecyclerView();  
  46.         loadJSON();  
  47.     }  
  48.   
  49.     private void initRecyclerView() {  
  50.   
  51.         mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);  
  52.         mRecyclerView.setHasFixedSize(true);  
  53.         RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());  
  54.         mRecyclerView.setLayoutManager(layoutManager);  
  55.     }  
  56.   
  57.     private void loadJSON() {  
  58.         HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
  59.         // set your desired log level  
  60.         logging.setLevel(HttpLoggingInterceptor.Level.BODY);  
  61.   
  62.         OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
  63.         // add your other interceptors …  
  64.         httpClient.addInterceptor(logging);    
  65.         RequestInterface requestInterface = new Retrofit.Builder()  
  66.                 .baseUrl(BASE_URL)  
  67.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  
  68.                 .addConverterFactory(GsonConverterFactory.create())  
  69.                 .client(httpClient.build())  
  70.                 .build().create(RequestInterface.class);             
  71.         mCompositeDisposable.add(requestInterface.getAllCountry()  
  72.                 .observeOn(AndroidSchedulers.mainThread())  
  73.                 .subscribeOn(Schedulers.io())  
  74.                 .subscribe(this::handleResponse,this::handleError));     
  75.     }  
  76.   
  77.     private void handleResponse(Country response) {  
  78.         if(response != null &&  
  79.                 response.getRestResponse() != null &&  
  80.                 response.getRestResponse().getResult() != null &&  
  81.                 response.getRestResponse().getResult().size() >0) {  
  82.             mAndroidArrayList = (ArrayList<result>) response.getRestResponse().getResult();  
  83.             mAdapter = new DataAdapter(mAndroidArrayList);  
  84.             mRecyclerView.setAdapter(mAdapter);  
  85.         }  
  86.     }  
  87.   
  88.     private void handleError(Throwable error) {  
  89.   
  90.         Toast.makeText(this"Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();  
  91.     }  
  92.   
  93.     @Override  
  94.     public void onDestroy() {  
  95.         super.onDestroy();  
  96.         mCompositeDisposable.clear();  
  97.     }  
  98. }  
  99. </result></result>  



You can Download the comple code From here: RetrofitWithRxSample

Hope this will helps you.
Enjoy Coding...... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.