我正在尝试创建注册新用户。但是,当我想注册时遇到错误retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall无法转换为com.example.test.dto.User我该如何解决?我是Retrofit和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
答案 0 :(得分:0)
您正在将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)
}
});