I am using a ProgressDialog to be presented before making a Rest request using Retrofit + RxJava, the response of the request is large and this is freezing the animation of ProgressDialog. How can I fix this? I only found examples saying to use runOnUiThread or the doInBackground with AsyncTask but, I'm using RxJava. I tried the runOnUiThread but it did not work.
//My request
public void getMyData(final MyListener listener) {
AppApi AppApi = getInstanceMyApi();
AppApi.getMyData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseData>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
//error
}
#Override
public void onNext(ResponseData response) {
//success, send data to presenter to update view
}
});
//Presenter call ws
public void attemptGetDataFromWS() {
showProgress();
getMyData(this);
}
#Override
public void onGetMyDataSuccess(ResponseData response) {
hideProgress();
}
#Override
public void onGetMyDataError(String error) {
hideProgress();
}
public void getMyData(final MyListener listener) {
AppApi AppApi = getInstanceMyApi();
AppApi.getMyData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<ResponseData>() {
#Override
public void onCompleted() {
dispose();
}
#Override
public void onError(Throwable e) {
listener.onGetMyDataError(e.getMessage());
}
#Override
public void onNext(ResponseData response) {
listener.onGetMyDataSuccess(response);
}
});
//Presenter call ws
public void attemptGetDataFromWS() {
showProgress();
getMyData(this);
}
#Override
public void onGetMyDataSuccess(ResponseData response) {
hideProgress();
}
#Override
public void onGetMyDataError(String error) {
hideProgress();
}
Follow this method .this is calling example with REST API + Retrofit
private void callRestAPI() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
newsAPI = retrofit.create(NewsAPI.class);
Call<JSONResponse> call = newsAPI.topNews("soure", "api-key");
// Set up progress before call
final ProgressDialog progressDoalog;
progressDoalog = new ProgressDialog(MainActivity.this);
progressDoalog.setMax(100);
progressDoalog.setMessage("Its loading....");
progressDoalog.setTitle("ProgressDialog bar example");
progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// show it
progressDoalog.show();
call.enqueue(new Callback<JSONResponse>() {
#Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
// close it after response
progressDoalog.dismiss();
}
}
#Override public void onFailure (Call < JSONResponse > call, Throwable t){
// close it after response
progressDoalog.dismiss();
}
});
}
Related
I created an Observable using the Observable.fromCallable method and subscribed to it as shown in the code snippet below.
Observable<String> stringObservable = Observable.fromCallable(new Callable<String>() {
#Override
public String call() throws Exception {
Thread.sleep(1000);
return Thread.currentThread().getName();
}
});
stringObservable.subscribeOn(Schedulers.io());
stringObservable.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(String aDouble) {
Toast.makeText(SimpleActivity.this, "onNext: " + aDouble,
Toast.LENGTH_LONG).show();
}
#Override
public void onError(Throwable e) {
new AlertDialog.Builder(SimpleActivity.this)
.setTitle("Error")
.setMessage(e.toString())
.show();
}
#Override
public void onComplete() {
}
});
The snippet above produced a toast showing that the Callable was run on the main thread instead of the Schedulers.io thread. What's happening?
Every operation on an Observable creates a new instance and does not effect the original one. Therefore
stringObservable.subscribeOn(Schedulers.io());
does not affect your code below.
The correct way of using them would be in a chain instead of using a variable.
Observable.fromCallable(new Callable<String>() {
#Override
public String call() throws Exception {
Thread.sleep(1000);
return Thread.currentThread().getName();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(String aDouble) {
Toast.makeText(SimpleActivity.this, "onNext: " + aDouble,
Toast.LENGTH_LONG).show();
}
#Override
public void onError(Throwable e) {
new AlertDialog.Builder(SimpleActivity.this)
.setTitle("Error")
.setMessage(e.toString())
.show();
}
#Override
public void onComplete() {
}
});
I use retrofit2 in my project. I have interface:
public interface ProductService {
#POST("findProducts")
Observable<ProductsResponse> getProducts();
}
and service method:
public ProductService getProductService() {
return getService(ProductService.class);
}
After that I use this service in my fragment. When I click to button I call this method:
#Override
public void onClick(View view) {
if (view.getId() == R.id.button) {
RestApiFactory.getInstance().getProductService().getProducts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new ProductHandler());
}
}
and realise handler in this fragment:
private class ProductHandler implements Observer<ProductsResponse> {
#Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe: start");
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onNext(ProductsResponse value) {
//save to DB in new Thread
}
#Override
public void onError(Throwable e) {
Log.d(TAG, "onError: "+ e.getMessage());
price.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
#Override
public void onComplete() {
Log.d(TAG, "onComplete: finish");
price.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
}
is it correct to implement ProductHandler implements Observer<ProductsResponse>'s methods
onSubscribe
onNext
onError
onComplete
in fragment?
I'm advised to move this in a singleton:
MySingleton.getInstanse().getData();
and move all to getData :
public void getData(){
RestApiFactory.getInstance().getProductService().getProducts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ProductsResponse>() {
#Override
public void onSubscribe(Disposable d) {
//send broadcast and catch in Fragment
}
#Override
public void onNext(ProductsResponse productsResponse) {
//send broadcast and catch in Fragment
}
#Override
public void onError(Throwable e) {
//send broadcast and catch in Fragment
}
#Override
public void onComplete() {
//send broadcast and catch in Fragment
}
});
}
but I think this is nonsense. who will tell you how to use it (in what place) Observable from retrofit?
there is no need to implement Observer.You can just need to create an observer for the same.Let's see the example.
service.getProducts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mObserver);
}
the observer should look like and must be initialized in oncreate:
mObserver = new Observer<ProductsResponse>() {
#Override
public void onSubscribe(Disposable d) {
mDisposableList.add(d);
}
#Override
public void onNext(ProductsResponse value) {
#Override
public void onError(Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
};
I am using MVP to decouple my view and model in my android application. I need to know how the model should feedback the result of the business logic to the view.
If for example a button is pressed to login, the activity would be like this, using butterknife #OnClick annotation:
#OnClick(R.id.login_button)
public void login() {
String email = mEmailEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
LoginCredentials loginCredentials = new LoginCredentials(email, password);
mPresenter.loginWithEmail(loginCredentials);
}
The presenter would then validate and make a request to the repository:
public void loginWithEmail(LoginCredentials loginCredentials) {
boolean isEmailValid = AuthValidator.validateEmail(loginCredentials.getUsername());
boolean isPasswordValid = AuthValidator.validatePassword(loginCredentials.getPassword());
if(isEmailValid && isPasswordValid) repository.loginEmailUser(loginCredentials);
if (!isEmailValid) view.handleInvalidEmail();
if (!isPasswordValid) view.handleInvalidPassword();
}
The repository would then execute the business logic:
#Override
public void loginEmailUser(LoginCredentials loginCredentials) {
Call<Token> call = userServiceApi.loginInToken(loginCredentials);
call.enqueue(new Callback<Token>() {
#Override
public void onResponse(#NonNull Call<Token> call, #NonNull Response<Token> response) {
if (response.isSuccessful()) {
// handle successful login
} else {
// Handle unsuccessful login
}
}
#Override
public void onFailure(#NonNull Call<Token> call, #NonNull Throwable t) {
// Handle failed request
}
});
Where the comments say // handle unsuccessful something, how does the model feedback to the view the outcomes of the business logic so that the view can handle these outcomes?
Thank you.
You can use a interface as callback, for example :
public interface RepositoryCallback {
void onLoginEmailUserSuccess(/*paramaters if you need*/);
void onLoginEmailUserError(/*paramaters if you need*/);
void onRequestFailed(/*paramaters if you need*/)
}
In the repository defined the listener
public class MyRepository {
private RepositoryCallback mListener;
#Override
public void loginEmailUser(LoginCredentials loginCredentials) {
Call<Token> call = userServiceApi.loginInToken(loginCredentials);
call.enqueue(new Callback<Token>() {
#Override
public void onResponse(#NonNull Call<Token> call, #NonNull Response<Token> response) {
if (response.isSuccessful()) {
// handle successful login
if (mListener != null) {
mListener.onLoginEmailUserSuccess()
}
} else {
// Handle unsuccessful login
if (mListener != null) {
mListener.onLoginEmailUserError()
}
}
}
#Override
public void onFailure(#NonNull Call<Token> call, #NonNull Throwable t) {
// Handle failed request
if (mListener != null) {
mListener.onRequestFailed()
}
}
});
public void setRepositoryCallback(RepositoryCallback listener) {
mListener = listener;
}
}
Then set the presenter as listener :
public class MyPresenter implements RepositoryCallback {
public void loginWithEmail(LoginCredentials loginCredentials) {
repository.setRepositoryCallback(this) // here or in the presenter constructor
boolean isEmailValid = AuthValidator.validateEmail(loginCredentials.getUsername());
boolean isPasswordValid = AuthValidator.validatePassword(loginCredentials.getPassword());
if(isEmailValid && isPasswordValid) repository.loginEmailUser(loginCredentials);
if (!isEmailValid) view.handleInvalidEmail();
if (!isPasswordValid) view.handleInvalidPassword();
}
#Override
public void onLoginEmailUserSuccess(//paramaters if you need){
// your code
}
#Override
public void onLoginEmailUserError(//paramaters if you need){
// your code
}
#Override
public void onRequestFailed(//paramaters if you need){
// your code
}
}
Hope this helps.
Sorry for my english.
I want to send multiple requests over the network and this tutorial
helped but i'm stuck at the latter part .
seems i'm expected to return a value(OrderValues) from onSubscribe,onNext,....
since apply function returns a value. But ....,onNext returns void by default.
Any help?Here is my piece of code
Observable<Restaurant> orderRestaurant= IdentityClient.getAPIService()
.getRestaurantById(restaurantId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<Menu> orderMenu= IdentityClient.getAPIService()
.getMenuById(menuId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<User> orderUser= IdentityClient.getAPIService()
.getUserById(userId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<OrderValues> combineValues=Observable.zip(orderRestaurant,
orderMenu, orderUser,
new Function3<Restaurant, Menu, User, OrderValues>() {
#Override
public OrderValues apply(Restaurant restaurant, Menu menu, User user)
throws Exception {
return new OrderValues(restaurant,menu,user);
}
I get an error here "cannot resolve method 'subscribe anonymous
org.reactivestreams.Subscriber(....OrderValues)
}).subscribe(new Subscriber<OrderValues>() {
#Override
public void onSubscribe(Subscription s) {
}
#Override
public void onNext(OrderValues orderValues) {
}
#Override
public void onError(Throwable t) {
}
#Override
public void onComplete() {
}
});
I'm assuming that you are using RxJava 2.
Use Observer instead of Subscriber. And also do not assign the result to a new Observable (you called it combineValues).
private void myMethod() {
Observable.zip(orderRestaurant, orderMenu, orderUser, new Function3<Restaurant, Menu, User, OrderValues>() {
#Override
public OrderValues apply(#NonNull Restaurant restaurant, #NonNull Menu menu, #NonNull User user) throws Exception {
return new OrderValues(restaurant, menu, user);
}
}).subscribe(new Observer<OrderValues>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(OrderValues orderValues) {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
}
});
}
}
Am doing a weather API call every 3secs and append the result to a textView using showWeather("weather") but I am sure there is a better way to do it. Am not sure why I need create Class Func1 but did because map required it. Also is there a way to shorten observer? I don't use lamda unfortunately. Any suggestions?
Observer myObserver = new Observer<String>() {
#Override
public void onError(Throwable e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
}
#Override
public void onComplete() {
}
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(String value) {
showWeather(value);
}
};
class Func1<T, T1> implements io.reactivex.functions.Function<Long, String > {
#Override
public String apply(Long aLong) throws Exception {
return getJSON("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1",300);
}
}
Observable.interval(3, TimeUnit.SECONDS)
.map(new Func1<Long, Observable<String>>() {
}).observeOn(AndroidSchedulers.mainThread()).subscribe(myObserver);
I tried :
Observable
.interval(3, SECONDS)
.subscribeOn(Schedulers.io())
.fromCallable(new Callable<String>() {
#Override
public String call() throws Exception {
return getJSON("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1", 300);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(myObserver)
But I get :
03-07 21:47:25.982 21181-21181/com.alex.rxandroidexamples E/imerExampleFragment$1$1: null
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
Also how do I unsubscribe onPause or onPause?
I found out the best way to do this is :
private final CompositeDisposable disposables = new CompositeDisposable();
Observable fetchWeatherInterval = Observable.interval(3, TimeUnit.SECONDS)
.map(new Function<Long, String>() {
#Override
public String apply(Long aLong) throws Exception {
return getWeather("http://samples.openweathermap.org/data/2.5/weather?", "London,uk", "b1b15e88fa797225412429c1c50c122a1");
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observer displayWeatherInterval = new Observer<String>() {
#Override
public void onError(Throwable e) {
Log.e("Throwable ERROR", e.getMessage());
}
#Override
public void onComplete() {
}
#Override
public void onSubscribe(Disposable d) {
disposables.add(d);
}
#Override
public void onNext(String value) {
textViewWeatherInterval.append(value);
}
};
buttonFetchIntervalWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fetchWeatherInterval.subscribe(displayWeatherInterval);
}
});