如何从Retrofit中的异步回调中获得最终结果?

时间:2016-07-10 17:21:59

标签: android asynchronous retrofit

我使用Retrofit 2在我的应用程序中实现REST。我的一个回调是异步的。我需要从onResponse()方法获取数据以传递,但此方法在结束时执行(在返回结果之后,当线程执行其他操作时)。我的代码中有一部分可以找到我的异步回调:

 public class JSONManager {

     //...

     public Result insertUser(final Context context, final String login, final String password, final String salt, final String email) {
            IUserApi userAPI = retrofit.create(IUserApi.class);

            Call<User> user = userAPI.insertUser(login, password, salt, email);
            user.enqueue(new Callback<User>() {

                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    try {
                         if (response.isSuccessful()) {
                            int mysqlId = response.body().mysqlId;
                            int temp = response.code();
                            if (mysqlId > 0) {
                                result.setResponseCode(1);
                                if (sqLiteManager.addUser(login, password, salt, email, mysqlId)) {
                                    result.setResponseCode(4);
                                    result.setResult(true);
                                } else {
                                    result.setResponseCode(5);
                                    result.setResult(false);
                                }
                            } else {
                                result.setResponseCode(2);
                                result.setResult(false);
                            }
                        } else {
                            result.setResponseCode(3);
                            result.setResult(false);
                        }
                    } catch (Exception e) {
                        result.setResult(false);
                        result.setResponseCode(3);
                        e.printStackTrace();
                    } 
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    result.setResponseCode(2);
                    result.setResult(false);
                }
            });

        return result;
    }

    //...
 }

在这里我尝试得到结果,但遗憾的是我没有收到预期的结果,因为我之前写过,user.enqueue()方法在执行onClickRegistrationButton()方法后执行(所以,为时已晚)。

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    //...   

    private void onClickRegistrationButton() {

            //...

            JSONManager jsonManager = new JSONManager();
            Result result = jsonManager.insertUser(getApplicationContext(), login, encryptedPassword, salt, email);

            //...
    }
}

你知道我该如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试调用将该Result作为参数传递给insertUseronResponse的函数,而不是尝试在onFailure方法中返回Result对象:

callNewFunction(Result result);

类似的东西:

public class JSONManager {

    //...

    public void insertUser(final Context context, final String login, final String password, final String salt, final String email) {
        IUserApi userAPI = retrofit.create(IUserApi.class);

        Call<User> user = userAPI.insertUser(login, password, salt, email);
        user.enqueue(new Callback<User>() {

            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                try {
                    if (response.isSuccessful()) {
                        int mysqlId = response.body().mysqlId;
                        int temp = response.code();
                        if (mysqlId > 0) {
                            result.setResponseCode(1);
                            if (sqLiteManager.addUser(login, password, salt, email, mysqlId)) {
                                result.setResponseCode(4);
                                result.setResult(true);
                            } else {
                                result.setResponseCode(5);
                                result.setResult(false);
                            }
                        } else {
                            result.setResponseCode(2);
                            result.setResult(false);
                        }
                    } else {
                        result.setResponseCode(3);
                        result.setResult(false);
                    }
                } catch (Exception e) {
                    result.setResult(false);
                    result.setResponseCode(3);
                    e.printStackTrace();
                }

                callNewFunction(result);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                result.setResponseCode(2);
                result.setResult(false);

                callNewFunction(result);
            }
        });
    }

    private void callNewFunction(Result result){
        Log.d(TAG, "callNewFunction: " + result);
    }

    //...
}