I'm using loaders to handle internet connections and it's working fine. Now I want to control the duration of the connection attempt, in other words to set a timeout.
I'm using the following code:
in onCreate
method:
Uri SearchUri =
Uri.parse(urlString);
Bundle args = new Bundle();
args.putParcelable(ARGS_URI, SearchUri);
// Initialize the Loader.
getLoaderManager().initLoader(LOADER_SEARCH,
args, this);
The other methods:
@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("")) {
...
}
}
So how can I set a timeout on the loader? For instance give a 10 sec duration, if there's no response yet, stop the attempt.