Search code examples
androidretrofit2

How to fix this error retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall cannot be cast to com.example.test.dto.User


I am trying to create registration new user. But when I want to sign up I have an error retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall cannot be cast to com.example.test.dto.User how can I fix it? I am a new in Retrofit and Android.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_sign_up);

    logo = (ImageView) findViewById(R.id.logo2);
    email2 = (EditText) findViewById(R.id.email2);
    password2 = (EditText) findViewById(R.id.password2);
    checkPassword2 = (EditText) findViewById(R.id.checkPassword2);
    emailLine2 = (View) findViewById(R.id.emailLine2);
    passwordLine2 = (View) findViewById(R.id.passwordLine2);
    checkPasswordLine2 = (View) findViewById(R.id.checkPasswordLine2);
    signUp2 = (Button) findViewById(R.id.signUp2);
    back = (Button) findViewById(R.id.backButton);

    animationItems();

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new TestTask().execute();
        }
    };

    signUp2.setOnClickListener(onClickListener);

}

public class TestTask extends AsyncTask<User, Void, User> {

    User user = new User(email2.getText().toString(),
            password2.getText().toString());

    @Override
    protected User doInBackground(User... users) {
        Call<User> call = RetrofitClient
                .getInstance()
                .getApi()
                .createUser(user);

        return (User) call;
    }

My log:

java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.ClassCastException: retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall cannot be cast to com.example.test.dto.User
    at com.example.test.SignUp$TestTask.doInBackground(SignUp.java:101)
    at com.example.test.SignUp$TestTask.doInBackground(SignUp.java:84)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234

Solution

  • You're casting a Call object to a User. This cast cannot succeed.

    I assume you want to execute the call. For that you'd need to call execute() and get the response:

    @Override
    protected User doInBackground(User... users) {
        Call<User> call = RetrofitClient
                .getInstance()
                .getApi()
                .createUser(user);
    
        return call.execute().body();
    }
    

    The problem with this approach is that it will only return a User if the call succeeds. There's no error handling. You need to check the return code and do some more checks before returning a User. You can have a look at https://square.github.io/retrofit/2.x/retrofit/retrofit2/Response.html

    Nevertheless, Retrofit already offers a way to execute the calls asynchronously without the need for an async task:

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            if (response.isSuccess()) {
                // can use response.body()
            } else {
                // might need to inspect the error body
            }
        }
    
        @Override
        public void onFailure(Call<User> call, Throwable t) {
            // executing the call failed (this is not an http error)
        }
    });