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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| private final Handler authHandler = new Handler() {
@Override
public void handleMessage(final Message msg) {
// on efface d'abord la boite de dialogue (progress)
progressDialog.dismiss();
if (msg.what == AuthToken) {
// traitement des données
} else if (msg.what == AuthEND) {
// traitement des données
} else if (msg.what == AuthERR) {
// erreur d'identification
Toast.makeText(getApplicationContext(), "Erreur d'identification, confirmez votre identifiant et/ou mot de passe !", Toast.LENGTH_LONG).show();
} else if (msg.what == AuthUnknownHostException) {
// erreur retournée par le serveur
Toast.makeText(getApplicationContext(), msg.obj + " !", Toast.LENGTH_LONG).show();
}
}
};
private void ConnexionSiteHTTPS() {
// Connexion avec les identifiants et récupération des données
// Affichage de la boite de dialogue
progressDialog = ProgressDialog.show(AgoraMenu.this, "Identification et récupération des données", "Connexion...");
progressDialog.setCancelable(false);
progressDialog.setIcon(R.drawable.acces);
// do the HTTP dance in a separate thread
new Thread() {
@Override
public void run() {
String errorMessage = null;
try {
// D'abord on fait un GET pour récupérer les données (phase non indispensable si accès simplement avec identifiants) :
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufferedReader = null;
try {
HttpGet httpGet = new HttpGet(ClientLoginURL);
HttpResponse httpResponse = httpclient.execute(httpGet);
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String ligneLue = bufferedReader.readLine();
while (ligneLue != null) {
stringBuffer.append(ligneLue);
stringBuffer.append("\n");
ligneLue = bufferedReader.readLine();
}
} catch (Exception e) {
Log.e("Mon programme", e.getMessage());
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
Log.e("Mon programme", e.getMessage());
}
}
}
// On récupère dans la page reçue le paramètre recherché
// On fait ensuite un POST avec les données récupérées
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("toto", "toto")); // ou tout autre paramètre nécessaire à la connexion
HttpPost httppost = new HttpPost(ClientLoginURL);
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// On accède maintenant à la page souhaitée
httppost = new HttpPost(autreURL);
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httppost);
entity = response.getEntity();
String content = "";
if (entity != null) {
content = EntityUtils.toString(entity);
}
} catch (UnknownHostException e) {
errorMessage = e.getMessage();
e.printStackTrace();
} catch (StringIndexOutOfBoundsException e) {
errorMessage = e.getMessage();
e.printStackTrace();
} catch (SocketTimeoutException e) {
errorMessage = e.getMessage();
e.printStackTrace();
} catch (ClientProtocolException e) {
errorMessage = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
errorMessage = e.getMessage();
e.printStackTrace();
} finally {
if (errorMessage != null) {
Message msg = authHandler.obtainMessage();
msg.what = AuthUnknownHostException;
msg.obj = errorMessage;
authHandler.sendMessage(msg);
}
}
}
}.start();
} |
Partager