Rien de spécial
Le blog de Régis

Appel de service web asynchrone

Lorsque l’on fait un appel web, la méthode execute() prend un certain temps. Elle fige l’interface graphique.

Pour éviter cela, il faut exécuter cette tâche longue dans un autre thread, et le framework Android offre la clase AsyncTask pour cela.

Screencast<figcaption class="wp-caption-text">La liste des incidents est maintenant rafraichie de façon asynchrone.</figcaption></figure>

Le code m’a pris une heure:

private class RefreshListTask extends AsyncTask<string , Integer, Boolean> {
		  
private ProgressDialog progressDlg;
		  
private Toast toast;

@Override
		  
protected Boolean doInBackground(String…; params) {
			  
String scope = params[0];
			  
boolean success = false;
			  
try {
				  
success = mProvider.refresh(scope);
			  
} catch (ClientProtocolException e) {
				  
Log.getStackTraceString(e);
				  
String s = getString(R.string.msg\_error\_clientprotocolexception);
				  
toast.setText(s);
				  
success = false;
			  
} catch (IOException e) {
				  
Log.getStackTraceString(e);
				  
String s = e.getLocalizedMessage();
				  
toast.setText(s);
				  
success = false;
			  
} catch (SAXException e) {
				  
Log.getStackTraceString(e);
				  
String s = getString(R.string.msg\_error\_saxparse);
				  
toast.setText(s);
				  
success = false;
			  
} catch (URISyntaxException e) {
				  
// should not happen
				  
Log.e(TAG, e.getMessage());
				  
Log.getStackTraceString(e);
			  
}
			  
return new Boolean(success);
		  
}

@Override
		  
protected void onPreExecute() {
			  
super.onPreExecute();
			  
progressDlg = new ProgressDialog(ListIncidentsActivity.this);
			  
progressDlg.show();
			  
toast = Toast.makeText(ListIncidentsActivity.this, « undefined »,
					  
Toast.LENGTH_LONG);
		  
}

@Override
		  
protected void onPostExecute(Boolean result) {
			  
super.onPostExecute(result);
			  
progressDlg.dismiss();

if (Boolean.TRUE.equals(result)) {
				  
// ListAdapter adapter=new IncidentListAdapter(provider);
				  
mAdapter.notifyDataSetChanged();
				  
if (mProvider.isEmpty()) {
					  
toast.setText(« Aucun incident connu actuellement »);
					  
toast.show();
				  
}
			  
} else {
				  
toast.show();
			  
}
		  
}
	  
}

Et même chose pour le poste d’un nouvel incident.

Le refactoring est obligatoire: le doInBackground() n’a pas accès aux éléments d’interface du thread principal — Il n’y a pas d’erreur à la compilation, mais un crash à l’exécution.</string>