android 如何修复此错误retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall无法强制转换为com.example.test.dto.User

13z8s7eq  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(150)

我试图创建注册新用户。但当我想注册我有一个错误retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall无法转换到com.example.test.dto.用户我怎么能修复它?我是一个新的改造和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;
    }

我的日志:

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
6jygbczu

6jygbczu1#

您正在将Call对象强制转换为User。此强制转换无法成功。
我假设你想执行这个调用。为此,你需要调用execute()并获得响应:

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

    return call.execute().body();
}

这种方法的问题是,如果调用成功,它只会返回User。没有错误处理。您需要检查返回代码,并在返回User之前进行更多检查。您可以查看https://square.github.io/retrofit/2.x/retrofit/retrofit2/Response.html
然而,Retrofit已经提供了一种异步执行调用的方法,而不需要异步任务:

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)
    }
});

相关问题