从 Retrofit2 onResponse 获取布尔值

Get boolean value from Retrofit2 onResponse

我正在尝试向服务器发出异步请求,这应该 return 我是对还是错。但问题是函数先returns false,然后retrofit函数才被触发。怎样才能在保持程序执行速度的同时得到自己需要的值呢? 我的代码:

private static boolean find;
public boolean checkAccountByPhone(String phone){

    Log.d("dbHandler", "function started...");

    DbHandler.getApi().checkAccountByPhone(phone).enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Log.d("dbHandler", "data is: "+response.body().equals("true"));

            // return response.body().equals("true");

            Log.d("dbHandler", "sent!");

        }
        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.d("dbHandler", t.getMessage());
        }
    });

    Log.d("dbHandler", "returned: "+DbHandler.find);
    return DbHandler.find; // always false???
}

可以修改Java中的逻辑,此代码仅供参考,避免回调

 fun checkAccountByPhone(phone: String):String{
    viewModelScope.launch(Dispatchers.IO) {
        val response : Response<String> = try {
            DbHandler.getApi().checkAccountByPhone(phone)
        } catch (ex: Exception) {
            ex.logException()
            null
        }

        //you can check response.body() here and can post either response.body() or DbHandler.find
        _yourLiveData.post(response.body() or DbHandler.find)


}