Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrofit RxJava
Build Gradles
===============
//Retrofit
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.6'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
//Retrofit logs
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
Usage
===============
//Simple
Observable<WSResponse<QuoteModel>> call = RetrofitUtils.getWebServices().getAllQuotes("1", 1);
RetrofitUtils.call(call, compositeDisposable, new RetrofitCallback<WSResponse<QuoteModel>>() {
@Override
public void onSuccess(WSResponse<QuoteModel> response) {
Log.d(TAG, "onSuccess() called with: response = [" + response + "]");
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError() called with: e = [" + e + "]");
}
});
//Hashmap
HashMap<String, Object >map=new HashMap<>();
map.put("device_id","1");
map.put("page","1");
call = RetrofitUtils.getWebServices().getAllQuotesHashMap(map);
RetrofitUtils.call(call, compositeDisposable, new RetrofitCallback<WSResponse<QuoteModel>>() {
@Override
public void onSuccess(WSResponse<QuoteModel> response) {
Log.d(TAG, "onSuccess() called with: response = [" + response + "]");
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError() called with: e = [" + e + "]");
Toast.makeText(HomeActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
//Raw Body
Request request=new Request();
request.device_id="1";
request.page="1";
call = RetrofitUtils.getWebServices().getAllQuotesRequest(request);
RetrofitUtils.call(call, compositeDisposable, new RetrofitCallback<WSResponse<QuoteModel>>() {
@Override
public void onSuccess(WSResponse<QuoteModel> response) {
Log.d(TAG, "onSuccess() called with: response = [" + response + "]");
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError() called with: e = [" + e + "]");
}
});
//Multipart
File file = FileUtils.getBackgroundFile(HomeActivity.this);
RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
call = RetrofitUtils.getWebServices().getAllQuotesPart("1", "1", filePart);
RetrofitUtils.call(call, compositeDisposable, new RetrofitCallback<WSResponse<QuoteModel>>() {
@Override
public void onSuccess(WSResponse<QuoteModel> response) {
Log.d(TAG, "onSuccess() called with: response = [" + response + "]");
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError() called with: e = [" + e + "]");
}
});
public interface RetrofitCallback<T> {
public void onSuccess(T response);
public void onError(Throwable e);
}
public class RetrofitUtils {
private static Retrofit wsRetrofit;
private static final Retrofit getWSRetrofit() {
if (wsRetrofit == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
wsRetrofit = new Retrofit.Builder()
.baseUrl(WebServices.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return wsRetrofit;
}
public static final WebServices getWebServices() {
return getWSRetrofit().create(WebServices.class);
}
public static <T> void call(Observable<T> call, final RetrofitCallback<T> callback) {
call(call, null, callback);
}
public static <T> void call(Observable<T> call, final CompositeDisposable disposableContainer, final RetrofitCallback<T> callback) {
call
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<T>() {
@Override
public void onSubscribe(Disposable d) {
if (disposableContainer != null)
disposableContainer.add(d);
}
@Override
public void onNext(T t) {
callback.onSuccess(t);
}
@Override
public void onError(Throwable e) {
callback.onError(e);
}
@Override
public void onComplete() {
}
});
}
}
public interface WebServices {
String BASE_URL = "url";
//For Simple
@GET("test.php")
Observable<WSResponse<QuoteModel>> getAllQuotes(@Query("device_id") String deviceId, @Query("page") int page);
//For Hashmap
@POST("test.php")
Observable<WSResponse<QuoteModel>> getAllQuotesHashMap(@QueryMap HashMap<String, Object> map);
//For Raw Data
@POST("test.php")
Observable<WSResponse<QuoteModel>> getAllQuotesRequest(@Body Request request);
//For Multipart
@Multipart
@POST("test.php")
Observable<WSResponse<QuoteModel>> getAllQuotesPart(@Part("device_id") String deviceId, @Part("page") String page, @Part MultipartBody.Part file);
}
public class WSResponse<T> {
@SerializedName("STATUS")
public String status;
@SerializedName("MESSAGE")
public String message;
@SerializedName("DATA")
public List<T> DATA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment