我试图创建注册新用户。但当我想注册我有一个错误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
1条答案
按热度按时间6jygbczu1#
您正在将
Call
对象强制转换为User
。此强制转换无法成功。我假设你想执行这个调用。为此,你需要调用
execute()
并获得响应:这种方法的问题是,如果调用成功,它只会返回
User
。没有错误处理。您需要检查返回代码,并在返回User
之前进行更多检查。您可以查看https://square.github.io/retrofit/2.x/retrofit/retrofit2/Response.html然而,Retrofit已经提供了一种异步执行调用的方法,而不需要异步任务: