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
|
class BigCalcul extends AsyncTask<NameValuePair,Void,JSONArray>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
RestoListe.this.progressDialog.setMessage("Chargement en cours");
RestoListe.this.progressDialog.show();
}
@Override
protected JSONArray doInBackground(NameValuePair ... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
if (params != null) {
for (NameValuePair p : params) nameValuePairs.add(p);
}
try {
HttpClient httpclient = new DefaultHttpClient();
String url = "http://www.test.com/test.php";
HttpPost httppost = new HttpPost(url);
nameValuePairs.add(new BasicNameValuePair("codepostal", codePostal));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
json= new JSONObject(EntityUtils.toString(response.getEntity()));
jArray1 = json.getJSONArray("requete1");
} catch (Exception ex) {
// signaler l'erreur
Log.e("MyAppTag","Erreur réception données", ex);
Toast.makeText(getBaseContext(),ex.getLocalizedMessage(),Toast.LENGTH_LONG).show();
// ou throw new Exception(); si il faut signaler l'erreur à l'appelant !
}
return jArray1;
}
@Override
protected void onPostExecute(JSONArray result) {
RestoListe.this.progressDialog.dismiss();
if (result != null)
RestoListe.this.onBigCalculResult(result);
}
}
public void onBigCalculResult(JSONArray result) {
// TODO Auto-generated method stub
//Création et initialisation de l'Adapter pour les personnes
AdapterPersoLivraison adapterProut = new AdapterPersoLivraison(this, result);
//Récupération du composant ListView
list = (ListView)findViewById(R.id.listResto);
//Initialisation de la liste avec les données
list.setAdapter(adapterProut);
}
public void startBigCalcul(String codePostal) {
BigCalcul back = new BigCalcul();
back.execute(new BasicNameValuePair("codepostal",codePostal));
} |