AsyncTask paramétrable et Webservices
Bonjour,
En bossant sur un projet, je me rend compte que je pourrai essayer de ne pas dupliquer certaines parties de mon code pour me faciliter la tâche. Cependant, je ne suis pas certain de comprendre comment faire. Je m'explique :
Pour appeler un webservice je fais comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
private OnClickListener OnClickListenerSeconnecter = new OnClickListener() {
@Override
public void onClick(View v)
{
new LogginIntoAppli().execute();
}
};
/**
* Async task to use WebServices
* */
private class LogginIntoAppli extends AsyncTask<Void, Void, String> {
//Before starting background thread Show Progress Dialog
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage(MainActivity.this.getResources().getText(R.string.chargement_des_donnees));
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(Void... arg) {
// Utilisation du webservice pour renvoyer le nouveau mot de passe à l'adresse saisie
ServiceHandler jsonParser = new ServiceHandler();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("postUserEmail", et_email.getText().toString()));
String json = jsonParser.makeServiceCall(URL.concat("login.php"), ServiceHandler.POST, params);
return json;
}
//After completing background task Dismiss the progress dialog
protected void onPostExecute(String json) {
super.onPostExecute(json);
if (pDialog.isShowing()) pDialog.dismiss();
try
{
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null)
{
if(!jsonObj.isNull("error"))
{
String res = jsonObj.get("error").toString();
if(res.equals("user_not_found")) // On fait comme si ça avait été envoyé pour ne pas donner d'indication à des gens mal intentionnés qui pourrait tenter de récupérer les adresses valides
{
Toast.makeText(MainActivity.this, getResources().getText(R.string.success_forgotten_password), Toast.LENGTH_LONG).show();
}
}
if(!jsonObj.isNull("resultState"))
{
String res = jsonObj.get("resultState").toString();
if(res.equals("ok"))
{
Toast.makeText(MainActivity.this, getResources().getText(R.string.success_forgotten_password), Toast.LENGTH_LONG).show();
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
} // Fin ResetPassword |
Mais je dois créer une classe (dans mon exemple : LogginIntoAppli) pour chaque webservice ce qui est assez mauvais.
Est-il possible de faire quelque chose du genre :
Code:
new maTacheAsynchron().execute("login.php", params, new fonction_s_executant_dans_onPostExecute(){ /*Portion de code à executer*/});
?
Voilà, si vous avez des tutoriels sous la main ou une idée je suis preneur.
Merci.