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

package android.com.retrofitwithrxsample;

import android.com.retrofitwithrxsample.adapter.DataAdapter;
import android.com.retrofitwithrxsample.model.Country;
import android.com.retrofitwithrxsample.model.Result;
import android.com.retrofitwithrxsample.network.RequestInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import com.newrelic.agent.android.NewRelic;

/**
 * Create by: Mukesh Yadav
 * www.androiddevelopersolutions.com
 */
public class MainActivity extends AppCompatActivity {

    private static String BASE_URL =  "http://services.groupkt.com/";
    private RecyclerView mRecyclerView;

    private CompositeDisposable mCompositeDisposable;

    private DataAdapter mAdapter;

    private ArrayList mAndroidArrayList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCompositeDisposable = new CompositeDisposable();
        initRecyclerView();
        loadJSON();
    }

    private void initRecyclerView() {

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private void loadJSON() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        // add your other interceptors …
        httpClient.addInterceptor(logging);  
        RequestInterface requestInterface = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build().create(RequestInterface.class);           
        mCompositeDisposable.add(requestInterface.getAllCountry()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this::handleResponse,this::handleError));   
    }

    private void handleResponse(Country response) {
        if(response != null &&
                response.getRestResponse() != null &&
                response.getRestResponse().getResult() != null &&
                response.getRestResponse().getResult().size() >0) {
            mAndroidArrayList = (ArrayList) response.getRestResponse().getResult();
            mAdapter = new DataAdapter(mAndroidArrayList);
            mRecyclerView.setAdapter(mAdapter);
        }
    }

    private void handleError(Throwable error) {

        Toast.makeText(this, "Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.clear();
    }
}



You can Download the comple code From here: RetrofitWithRxSample

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

 

Copyright @ 2013 Android Developer Blog.