我使用加载程序处理互联网连接并且工作正常。现在我想控制连接尝试的持续时间,换句话说就是设置超时。
我使用以下代码:
onCreate
方法中的:
Uri SearchUri =
Uri.parse(urlString);
Bundle args = new Bundle();
args.putParcelable(ARGS_URI, SearchUri);
// Initialize the Loader.
getLoaderManager().initLoader(LOADER_SEARCH,
args, this);
其他方法:
@Override
public Loader<RESTLoader.RESTResponse> onCreateLoader(int i, Bundle args) {
a = args;
Log.v("TESTING","in OnCreateLoader");
if (args != null && args.containsKey(ARGS_URI)){
Log.v("TESTING","in OnCreateLoader, getting communication key");
Uri action = args.getParcelable(ARGS_URI);
return new RESTLoader(this, RESTLoader.HTTPVerb.GET,action);
}
return null;
}
@Override
public void onLoadFinished(Loader<RESTLoader.RESTResponse> loader, RESTLoader.RESTResponse data) {
int code = data.getCode();
String json = data.getData();
if (code == 200){
Log.v("TESTING","Inside onLoadFinished, code = 200");
}
else {
Log.v("TESTING","Inside onLoadFinished, code != 200");
Toast.makeText(getApplicationContext(), "Connection Problem", Toast.LENGTH_SHORT).show();
}
// Check to see if we got an HTTP 200 code and have some data.
if (code == 200 && !json.equals("")) {
...
}
}
那么如何在加载器上设置超时?例如,给出10秒的持续时间,如果还没有响应,则停止尝试。