Aller au contenu

retour variable int d'un AsyncTask


fistonfromcaen

Recommended Posts

Bonjour à tous,


 


j'ai besoin de votre aide.


 


Je me sers d'un AsyncTask afin de faire une requête INSERT INTO sur ma bdd mysql et dans ce même AsyncTask, je lance une requête GET afin de récupérer l'id de la ligne qui vient d'être créé. Je passe ensuite l'id récupéré dans une variable globale. Tout cela suite à l'appui sur un bouton.


 


Mon problème est que l'id que je récupère est toujours celui de l'enregistrement précédent. Exemple, j'enregistre un train 3355 avec la date du 02/02/2014 avec pour id 12. Le logcat me donne bien comme id 12 et la boite de dialogue dans mon appli va me retourner 11. 


 


Je vous laisse une partie de mon code, si une âme charitable a une piste à me donner :



/*
* Async task de creation de nouveau train
*
* */
private class AddNewTrain extends AsyncTask<String, Void, Void> {
String newTrain;
String newDateTrain;
int idTrain;

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CreationTrain.this);
pDialog.setMessage("Enreistrement d'un nouveau train..");
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected Void doInBackground(String... arg) {

/*
*
* Enregistrement du train qui vient d'être créé
*
*/
newTrain = arg[0];
String newObsTrain = arg[1];
newDateTrain = arg[2];
String newControleOkTrain = arg[3];

// Preparing post params
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("train_numtrain", newTrain));
params.add(new BasicNameValuePair("train_obs", newObsTrain));
params.add(new BasicNameValuePair("train_date", newDateTrain));
params.add(new BasicNameValuePair("train_controle", newControleOkTrain));

ServiceHandler serviceClient = new ServiceHandler();

String json = serviceClient.makeServiceCall(URL_NEW_TRAIN,
ServiceHandler.POST, params);

Log.d("Create Response: ", "> " + json);

if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
boolean error = jsonObj.getBoolean("error");

// checking for error node in json
if (error) {
Log.e("Creation Train Erreur: ", "> " + jsonObj.getString("message"));
}
} catch (JSONException e) {
e.printStackTrace();
}

} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}

ServiceHandler jsonParser = new ServiceHandler();
String jsonGet = jsonParser.makeServiceCall(URL_GET_IDNEWTRAIN, ServiceHandler.GET,params);

Log.e("Response: ", "> " + jsonGet);

if (jsonGet != null) {
try {
JSONObject jsonGetObj = new JSONObject(jsonGet);
if (jsonGetObj != null) {
/*
*on retourne l'id du train qui vient d'être ajouter à la bdd
*/
JSONArray idtrain = jsonGetObj
.getJSONArray("train");

for (int i = 0; i < idtrain.length(); i++) {
JSONObject idtrainObj = (JSONObject) idtrain.get(i);
Train id = new Train();
id.setTrainId(idtrainObj.getInt("train_id"));
idTrain = id.getTrainId();
}
/*
*
*/
}

} catch (JSONException e) {
e.printStackTrace();
}

} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
idNewTrain = idTrain;
//new GetIdNewTrain().execute(newTrain,newDateTrain);
}
}

public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position).toString() + " Selected",Toast.LENGTH_LONG).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
}
/*
* fin Async task de creation de nouveau train
*/


Lien vers le commentaire
Partager sur d’autres sites

Juste un truc qui m'interpelle, pourquoi utilises-tu une variable globale pour transférer le résultat ?

 

Tu pourrais, par exemple, utiliser une AsyncTask<String, Void, Train> et récupérer ton nouveau Train dans OnPostExecute(Train newTrain) puis extraire l'identifiant newTrain.getTrainId() là directement là où tu en as besoin =)

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...