我想知道是否可以从被调用方法中包含的函数返回一个值。
此方法正在获取 html 页面源代码,以便有时间返回值。
但时间不是这里的问题(或者可能是)。
protected boolean booleanLinkReturn(String link) {
boolean ret = false;
Ion.with(getApplicationContext()).load(link).asString().setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
ret = true;
/*
if I set "ret" here (I can't do btw because it needs to be declared final)
the function will return always false
p.s. I have to do more things here not only the boolean
*/
}
});
return ret;
}
有没有办法在方法内代码的结果处返回基数中的 boolean 值?
最佳答案
不,因为现在没有调用该函数。它在未来的某个时间被调用(或者永远不被调用也是一种可能性)。无法返回该值,因为它尚未计算,而且可能永远不会计算。
执行此操作的正确方法是,将需要使用已知值运行的所有代码放置在 onCompleted 中,或通过另一个回调函数从那里调用。
关于java - 从被调用方法内的函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49523824/