Manjunath Manjunath - 1 month ago 29
Android Question

Retrofit call in non-activity class

I am beginner to android, am using Retrofit 2.0 for http calls. Since, I use same call in different activities, I created the function in non-activity class.

Here my retrofit calls in non-activity class code,

public class ServerRequests {


private static ServerRequests serverRequests = new ServerRequests();

public static ServerRequests getInstance(){

return serverRequests;
}

public LoginResponse ClientLogin(final LoginRequest request, Context context){

final ProgressDialog dialog = DialogueUtils.getInstance().showProgressDialog(context);
dialog.setMessage("Loading...");
dialog.show();

RestApi mApi = RetrofitProvider.getInstance().getRestApi();

Call<UserToken> call = mApi.clientLogin(request);

// Stores Login response
final LoginResponse loginResponse = new LoginResponse();

call.enqueue(new Callback<UserToken>() {
@Override
public void onResponse(Call<UserToken> call, Response<UserToken> response) {
dialog.dismiss();
if (response.isSuccessful()){
loginResponse.setMloginstatus(true);
loginResponse.setStatusCode(response.code());
loginResponse.setUserToken(response.body());

return;
}

// response isn't successful
loginResponse.setMloginstatus(false);
loginResponse.setStatusCode(response.code());
loginResponse.setMessage(response.message());
loginResponse.setUserToken(null);
}

@Override
public void onFailure(Call<UserToken> call, Throwable t) {

dialog.dismiss();
loginResponse.setMloginstatus(false);
loginResponse.setUserToken(null);
loginResponse.setMessage(t.getMessage());
loginResponse.setStatusCode(FAILURE_ERROR);
}
});

return loginResponse;
}
}


I will make call to above function in Activity class,
Here is the code,

LoginRequest request = new LoginRequest();
request.setPassword(PASSWORD);
request.setEmail(USER_NAME);

// Calling Login function

LoginResponse response = ServerRequests.getInstance().ClientLogin(request, this);


Here, before receiving response in activity class, next set of code lines are executing. So, Activity class doesn't waiting till function to return back to the calling sequence.

Can anyone please suggest me what's the better approach to do this.

Thank you

Answer

I think you can use a interface to handle callback.

Step1: Define an interface

public interface LoginListener{
 public void success(Response<UserToken> response);
 public void failed(String message);
}

Step2: Use this interface on the method that you use retrofit.

public LoginResponse ClientLogin(final LoginRequest request, Context context, LoginListener listener){

    final ProgressDialog dialog = DialogueUtils.getInstance().showProgressDialog(context);
    dialog.setMessage("Loading...");
    dialog.show();

    RestApi mApi = RetrofitProvider.getInstance().getRestApi();

    Call<UserToken> call = mApi.clientLogin(request);

    // Stores Login response
    final LoginResponse loginResponse = new LoginResponse();

    call.enqueue(new Callback<UserToken>() {
        @Override
        public void onResponse(Call<UserToken> call, Response<UserToken> response) {
            dialog.dismiss();

            listener.success(response);
        }

        @Override
        public void onFailure(Call<UserToken> call, Throwable t) {

            dialog.dismiss();
            listener.failed("message error");
        }
    });

    return loginResponse;
}

I hope, This way can help you.