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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| bg_insertion bg = new bg_insertion(getApplicationContext());
bg.execute(des);
// Traitement de sauvegarde tache
private class bg_insertion extends AsyncTask<String, Void, String> {
AlertDialog dialog;
Context context;
public bg_insertion(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("insertion");
}
@Override
protected String doInBackground(String... strings) {
String result2 = "";
String ddt = strings[0];
String connstr2 = "http://10.13.100.67/TDBPROD/android/suivi.php";
try {
URL url = new URL(connstr2);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));
String data = URLEncoder.encode("desxc", "UTF-8") + "=" + URLEncoder.encode(ddt, "UTF-8");
writer.write(data);
writer.flush();
writer.close();
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "ISO-8859-1"));
String ligne2 = "";
while ((ligne2 = reader.readLine()) != null) {
result2 = result2 + ligne2;
// ou bien result += ligne;
}
reader.close();
ips.close();
http.disconnect();
return result2;
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result2;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
dialog.setMessage(s);
//dialog.show();
if (s.contains("succes insertion")) {
Toast.makeText(context, " inséré avec succès ", Toast.LENGTH_LONG).show();
//finish();
// startActivity(getIntent());
} else {
Toast.makeText(context, s, Toast.LENGTH_LONG).show();
}
}
} |
Partager