Android 如何处理异步操作的返回值

Android 如何处理异步操作的返回值,android,asynchronous,retrofit,Android,Asynchronous,Retrofit,我正在为我的Android应用程序开发注册功能。函数调用链是RegisterActivity->RegisterViewModel->UserRepository->DataSource,并通过DataSource register()返回值处理结果。改型用于访问url,但register()是一个异步函数,处理结果时遇到问题: 这是DataSource.register(): 公共结果寄存器(字符串用户名、字符串代码、字符串密码){ 试一试{ 改装改装=新改装.Builder() .clien

我正在为我的Android应用程序开发注册功能。函数调用链是RegisterActivity->RegisterViewModel->UserRepository->DataSource,并通过DataSource register()返回值处理结果。改型用于访问url,但register()是一个异步函数,处理结果时遇到问题: 这是DataSource.register():

公共结果寄存器(字符串用户名、字符串代码、字符串密码){
试一试{
改装改装=新改装.Builder()
.client(CookieClient.getClient(AppUtils.getAppContext()))
.baseUrl(ApiConstants.AUTH_BASE_URL)
.addConverterFactory(FastJsonConverterFactory.create())
.build();
AuthService AuthService=reformation.create(AuthService.class);
Call Call=authService.postSignUp(用户名、代码、密码);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//成功
if(response.code()==200){
result=新的result.Success(response.body());
}
否则{
结果=新的result.Fail(response.code());
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
结果=新结果。错误(新IOException(t));
}
});
返回结果;
}捕获(例外e){
返回新结果。错误(e);
}
}

从代码中退一步,解释你希望应用程序做什么。如果我是用户并打开应用程序,我会看到什么?我能做什么?当我做那个动作时会发生什么?
    public Result register(String username,String code, String password){
        try{
            Retrofit retrofit = new Retrofit.Builder()
                    .client(CookieClient.getClient(AppUtils.getAppContext()))
                    .baseUrl(ApiConstants.AUTH_BASE_URL)
                    .addConverterFactory(FastJsonConverterFactory.create())
                    .build();
            AuthService authService = retrofit.create(AuthService.class);
            Call<LoggedInUser> call = authService.postSignUp(username, code, password);

            call.enqueue(new Callback<LoggedInUser>() {
                @Override
                public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {
                    //success
                    if(response.code()==200){
                    
                        result = new Result.Success<>(response.body());
                    }
                    else{
                        result = new Result.Fail(response.code());
                    }
                }

                @Override
                public void onFailure(Call<LoggedInUser> call, Throwable t) {
                    result =  new Result.Error(new IOException(t));
                }
            });
            return result;

        } catch (Exception e){
            return new Result.Error(e);
        }
    }