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
   | package com.proj.tmj;
 
import java.util.ArrayList;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class LoginActivity extends Activity {
 
	EditText un,pw;
	TextView error;
    Button login;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
 
        un=(EditText)findViewById(R.id.edittext_un);
        pw=(EditText)findViewById(R.id.edittext_pw);
        login=(Button)findViewById(R.id.button_login);
        login.setOnClickListener(new View.OnClickListener()
        {
        	public static final String MY_PREFERENCES = "mespreferences";
            public void onClick(View v) {
            	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            	postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            	postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            	String response = null;
            	try {
            	    response = Httpclient.executeHttpPost("http://10.0.2.2/mesRequetes/login.php", postParameters);
            	    String res=response.toString();
            	   // res = res.trim();
            	    res= res.replaceAll("\\s+","");         	              	 
            	    //error.setText(res);
 
            	    if(res.equals("1"))
            	    {
 
            	    	AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this);
            	    	adb.setIcon(R.drawable.succes);
                		adb.setTitle("Succès");
                		adb.setMessage("Connexion réussie");
 
                		//on affiche la boite de dialogue
 
                		adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
 
							public void onClick(DialogInterface arg0, int arg1) {
								// TODO Auto-generated method stub
								//mon champ 
		            	        EditText usern = (EditText) findViewById(R.id.edittext_un);
		            	        // ma valeur
		            	        String loginStr = usern.getText().toString();
		            	        EditText passw = (EditText) findViewById(R.id.edittext_pw);
		            	        String passStr = passw.getText().toString(); 
 
		            	        SharedPreferences settings = getSharedPreferences(MY_PREFERENCES, 0);
		        				SharedPreferences.Editor editor = settings.edit();
		        				editor.putString("username", loginStr);
		        				editor.putString("password", passStr);
		        			    editor.commit();
							}
                		 });
                		adb.show();
            	    }
            	    else
            	    	{
            	    	AlertDialog.Builder adb2 = new AlertDialog.Builder(LoginActivity.this);
            	    	adb2.setIcon(R.drawable.echec);
            		adb2.setTitle("Echec de connexion");
            		adb2.setMessage("Nom d'utilisateur et/ou mot de passe incorrect(s)");
            		adb2.setNegativeButton("Réessayez", null) ;
            		adb2.show();
            	}
            	} catch (Exception e) {
            		un.setText(e.toString());
            	}
            }
        });
    } 
 
} | 
Partager