'Return data from retrofit onResponse

Im doing a basic android app that performs a simple login againts an API. I need to return the received data but the retrofit onResponse method has void return type. My code is:

package com.example.deberesloginjava.data;

import android.util.Log;

import com.example.deberesloginjava.data.APIServices.Post;
import com.example.deberesloginjava.data.model.LoggedInUser;

import java.io.IOException;

import com.example.deberesloginjava.data.APIServices.APIService;
import com.example.deberesloginjava.data.APIServices.ApiUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.content.ContentValues.TAG;

/**
 * Class that handles authentication w/ login credentials and retrieves user information.
 */

public class LoginDataSource {

    private APIService mAPIService;

    public void getData(Callback<Result<LoggedInUser>> callback){
        apiClient.getData().enqueue(callback);
    }

    public Result<LoggedInUser> login(String username, String password) {

        try {

            mAPIService = ApiUtils.getAPIService();

           mAPIService.login(username, password, "password").enqueue(new Callback<Post>() {
                @Override
                public void onResponse(Call<Post> call, Response<Post> response) {
                    if(response.isSuccessful()) {
                        LoggedInUser loggedInUser;
                        loggedInUser = new LoggedInUser(response.body().getUserName(),response.body().getUserName(), response.body().getAccess_token());
                        return new Result.Success<>(loggedInUser);
                    }
                }



                @Override
                public void onFailure(Call<Post> call, Throwable t) {
                    Log.e(TAG, "Unable to submit post to API.");
                }
            });

        } catch (Exception e) {
            return new Result.Error(new IOException("Error logging in", e));
        }
    }
}

I have found at least two questions like this and both have almost the same solution with callbacks but im not able to replicate it in my case.

Thanks in advance.



Solution 1:[1]

Actually you cant return a value , instead you can try making a method for passing/storing data in DTO & call that method in your respose.IsSuccessful. Pass the object of response json to the method for storing your required stuffs like token, username n etc,.

You can try making it like

               if(response.isSuccessful()) {
                    // just a loader untill all operations are done
                    progressBar.SetVisibility(View.Visible);
                    //Make a toast here for successful login
                    LoggedInUser loggedInUser;
                    loggedInUser = new LoggedInUser(response.body().getUserName(),response.body().getUserName(), response.body().getAccess_token());
                    UserDTO(loggedInUser);//it will save the users login details.
                }
                else{
                    //Make a toast as per your responce error.code as Invalid Password or any other error
                }

In UserDTO() you can create a login session for the user for auto login when user uses the app for next time, he doesnt need to enter ID & Password again. After all processing Navigate to your desire View StartActivity()

Hope this is what you are trying to achieve.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Blu