android - 如何修复此错误 retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall 无法转换为 com.example.test.dto.User
问题描述
我正在尝试创建注册新用户。但是当我想注册时,我有一个错误 retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall cannot be cast to 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
解决方案
您正在将Call
对象转换为User
. 这个演员不能成功。
我假设你想执行调用。为此,您需要致电execute()
并获得回复:
@Override
protected User doInBackground(User... users) {
Call<User> call = RetrofitClient
.getInstance()
.getApi()
.createUser(user);
return call.execute().body();
}
这种方法的问题是它只会User
在调用成功时返回 a 。没有错误处理。您需要检查返回码并在返回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)
}
});
推荐阅读
- laravel - 如何从两个表中获取匹配的数据?
- python - 从多个 JSON 文件构造 DataFrame
- typescript - 如何使用多态性来隐藏接口的属性?
- python - pip 安装 math3d - 错误
- java - Hadoop 失败错误:java.lang.ArrayIndexOutOfBoundsException:5
- javascript - 您如何访问“全选”复选框?
- discord - 这些用户是机器人还是人?
- c# - 如何声明泛型类的实例变量?
- c++ - qInputDialog 在全屏应用程序中使 Unity 任务栏和标题栏可见
- c# - 当函数属于必须解析的类时,如何使用 Autofac 注册委托或函数?