但就我而言,我延长了
AsyncTaskLoader
覆盖了
loadInBackground()
等等等等。
onLoadFinished()
通常调用。
但我的问题发生在网络错误的情况下。如果数据为空
onLoadFinished()
立即调用,无需网络调用。
在任何一种情况下,给定的回调都与加载程序相关联,并且
将在加载程序状态更改时调用。如果在呼叫点
存在并已生成其数据,然后回调
onLoadFinished(Loader,D)将立即调用(在此内部
restartLoader()
在我的活动中,而不是
initLoader()
我将代码的重要部分发布在下面:
在主要活动中:
protected void postJson(int loaderId, Parcelable object, boolean loadOffline) {
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.OBJ, object);
bundle.putBoolean(Constants.OFFLINE, loadOffline);
loaderManager.initLoader(loaderId, bundle, this);
}
@Override
public Result loadInBackground() {
Constants.verbose("loadInBackground Called");
.....
Response tempResult;
try {
tempResult = loadFromNetwork();
........
} catch (IOException e) {
Constants.error("Network Error for loader Id: " + loaderId, e);
return null;
}
}
@Override
public void deliverResult(Result result) {
Constants.verbose("deliverResult called");
super.deliverResult(result);
}
onLoadFinished()
活动中
@Override
public void onLoadFinished(Loader loader, NetworkAsyncTaskLoader.Result data) {
hideProgress();
Constants.debug("Load finished for: " + loader.getId());
.....
}
07-31 10:08:02.177 27677-28061/com.sample.test D/OkHttp: <-- HTTP FAILED: java.net.ConnectException: Failed to connect to ...
07-31 10:08:02.179 27677-28061/com.sample.test E/Test: Network Error for loader Id: 101
java.net.ConnectException: Failed to connect to ...
07-31 10:08:02.192 27677-27677/com.sample.test V/Test: deliverResult called
07-31 10:08:02.193 27677-27677/com.sample.test D/Test: Load finished for: 101
07-31 10:08:08.982 27677-27677/com.sample.test D/Test: Load finished for: 101
initLoader()
或者尝试发起网络呼叫。它直接呼叫
onLoadFinished()
更新:
restartLoader()