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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
   | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button SeConnecter;
    private TextView YourEmail, YourPassword;
    private EditText MyEmail, MyPassword;
    private ImageView imgAlstom;
    private static Candidat candidatConnecte = null;
 
    public static void setCandidatConnecte (Candidat unCandidat)
    {
        candidatConnecte = unCandidat;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        this.MyEmail = (EditText) findViewById(R.id.idMail);
        this.MyPassword = (EditText) findViewById(R.id.idPassword);
        this.YourEmail = (TextView) findViewById(R.id.textView13);
        this.YourPassword = (TextView) findViewById(R.id.textView16);
        this.imgAlstom = (ImageView) findViewById(R.id.idAlstomMain);
 
        // Construction du bouton suivant
        this.SeConnecter = (Button) findViewById(R.id.idSeConnecter);
        //rendre le bouton Se Connecter ecoutable
        this.SeConnecter.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
 
 
        if(v.getId() == R.id.idSeConnecter){
            // Si clic sur le bouton SeConnecter
            String mail = this.YourEmail.getText().toString();
            String password = this.YourPassword.getText().toString();
            // renvoi des données
 
            //instancier un candidat
            Candidat unCandidat = new Candidat(mail, password);
            //verification de la presence du candidat en base
            Connexion uneConnexion = new Connexion();
            //lancement de la tache asynchrone avec le candidat qui se connecte
            uneConnexion.execute(unCandidat);
 
            //verification de la connexion
            if (candidatConnecte == null )
            {
                Toast.makeText(this, "Veuillez vérifier vos identifiants",
                        Toast.LENGTH_LONG).show();
                // (Context context, CharSequence text, int duration)
                this.YourEmail.setText("");
                this.YourPassword.setText("");
            }else
             {
                Toast.makeText(this, "Bienvenue "+candidatConnecte.getNom(),
                        Toast.LENGTH_LONG).show();
 
                Intent unIntent = new Intent(this, Menu.class);
            /* putExtra = récupérer les valeurs de la page MainActivity
                alstom
                pour les retrouver dans la page Menu
            */
                unIntent.putExtra("idCandidat", candidatConnecte.getIdcandidat()+"");
                unIntent.putExtra("nom", unCandidat.getNom());
                unIntent.putExtra("prenom", unCandidat.getPrenom());
                this.startActivity(unIntent);
            }
        }
    }
}
 
/*************** Tache asynchrone ********************/
/* 3 parametres : entrée, progression, sortie
  elle recoit un candidat (email et mdp) et renvoie
 * un candidat avec tous les données s'il est présent
 * sinon renvoie null.
 */
 
class Connexion extends AsyncTask<Candidat,Void, Candidat>
{
    //tache de fond: executée en arrière-plan.
    @Override
    protected Candidat doInBackground(Candidat... candidats) {
        Candidat candidatBDD = null;
        Candidat candidatTest = candidats[0]; //premier parametre(NOM/PRENOM)
        String url = "http://localhost/Cfa_Insta/mysurveyalstom/verifConnexion.php";
        String resultatJson ="";
        //construction de l'URL
        url += "?nom="+candidatTest.getNom()+"&prenom="+candidatTest.getPrenom();
        try{
            //instanciation la classe URL pour la page php webservice
            URL uneUrl = new URL(url);
            //ouverture du client HTTP
            HttpURLConnection uneConnexion = (HttpURLConnection) uneUrl.openConnection();
            //parametrage de la connexion
            uneConnexion.setRequestMethod("GET");
            uneConnexion.setDoInput(true);
            uneConnexion.setDoOutput(true);
            //on fixe le temps d'attente
            uneConnexion.setConnectTimeout(20000);
            // 2 secondes
            uneConnexion.setReadTimeout(15000);
            // 1,5 seconde
            //etablissement de la connexion
            uneConnexion.connect();
 
            //lecture des donnees JSON Résultats à partir de la page PHP
            InputStreamReader isr = new InputStreamReader(uneConnexion.getInputStream(), "UTF-8");
            //la lecture de la page se fait dans un buffer : memoire tompon
            BufferedReader br = new BufferedReader(isr);
            //instanciation d'une chaine de caractères dynamique
            StringBuilder sb = new StringBuilder();
            String ligne ="";
            //lecture des chaines contenues dans la page
            while ((ligne = br.readLine())!=null)
            {
                sb.append(ligne);
            }
            resultatJson = sb.toString(); //resultat des chaines lues
            isr.close();
            br.close();
            Log.e("JSON : ", resultatJson);
        }
        catch(Exception exp){
            Log.e("Erreur : ", "Erreur de connexion à url :"+url);
            exp.printStackTrace();
        }
 
        //extraire le contenu JSON en un candidat
        try{
            JSONArray tabJson = new JSONArray(resultatJson);
            //un seul resultat JSON: personne connectee
            JSONObject unObjet = tabJson.getJSONObject(0);
            candidatBDD = new Candidat (
                    unObjet.getInt("idcandidat"),
                    unObjet.getString("nom"), unObjet.getString("prenom"),
                    unObjet.getString("metier"),
                    candidatTest.getMail(), candidatTest.getPassword(),
                    unObjet.getString("lieu"), unObjet.getString("anciennete"),
                    unObjet.getString("lesReponses")
            );
            Log.e("Candidat: ", candidatBDD.toString());
        }
        catch(JSONException exp){
            Log.e("Erreur Json : ", "Impossible de parser le json");
        }
 
 
        return candidatBDD;
    }
 
    @Override
    protected void onPostExecute(Candidat candidat) {
        /*
        le resultat de la tache asynchrone : candidatBDD va valoriser
        le candidat connecte de la classe MainActivity
         */
        MainActivity.setCandidatConnecte(candidat);
    }
} | 
Partager