Created
November 6, 2017 18:25
-
-
Save pankaj89/18d8e101c2f5dcab73e797bfdd47333e to your computer and use it in GitHub Desktop.
Retrofit RxJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface RetrofitCallback<T> { | |
public void onSuccess(T response); | |
public void onError(Throwable e); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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