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 163 164 165 166 167 168 169
| package com.mbs.acmsmobilesoft;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ConnexionActivity extends Activity{
// Lien vers votre page php sur votre serveur
//private static final String UPDATE_URL = "http://192.168.249.1/ACMS_Project/connexion.php";
Button login;
String name="",pass="";
EditText username,password;
TextView tv;
byte[] data;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
InputStream inputStream;
SharedPreferences app_preferences ;
List<NameValuePair> nameValuePairs;
CheckBox check;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.connexion_layout);
// initialisation d'une progress bar
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Connexion en cour...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.connexion);
check = (CheckBox) findViewById(R.id.check);
String Str_user = app_preferences.getString("username","0" );
String Str_pass = app_preferences.getString("password", "0");
String Str_check = app_preferences.getString("checked", "no");
if(Str_check.equals("yes")){
username.setText(Str_user);
password.setText(Str_pass);
check.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
name = username.getText().toString();
pass = password.getText().toString();
String Str_check2 = app_preferences.getString("checked", "no");
if(Str_check2.equals("yes")){
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("username", name);
editor.putString("password", pass);
editor.commit();
}
if(name.equals("") || pass.equals("")){
Toast.makeText(ConnexionActivity.this, "Blank Field..Please Enter", Toast.LENGTH_LONG).show();
createDialog("Erreur", "Entrez le nom utilisateur et ou le mot de passe");
}
else{
progressDialog.show();
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.249.1/ACMS_Project/connexion.php");
// Add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data)) ){
buffer.append(new String(data, 0, len));
}
inputStream.close();
}
catch (Exception e){
Toast.makeText(ConnexionActivity.this, "error"+e.toString(), Toast.LENGTH_LONG).show();
}
if(buffer.charAt(0)=='Y'){
progressDialog.dismiss();
Toast.makeText(ConnexionActivity.this, "login successfull", Toast.LENGTH_LONG).show();
Toast.makeText(ConnexionActivity.this, "res "+buffer.charAt(0)+",", Toast.LENGTH_LONG).show();
}
else{
progressDialog.dismiss();
Toast.makeText(ConnexionActivity.this, "Invalid Username or password", Toast.LENGTH_LONG).show();
Toast.makeText(ConnexionActivity.this, "res "+buffer.charAt(0)+",", Toast.LENGTH_LONG).show();
createDialog("Erreur", "Nom utilisateur/mot de passe incorrecte");
}
}
}
});
check.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// Perform action on clicks, depending on whether it's now checked
SharedPreferences.Editor editor = app_preferences.edit();
if (((CheckBox) v).isChecked()){
editor.putString("checked", "yes");
editor.commit();
}else{
editor.putString("checked", "no");
editor.commit();
}
}
});
}
public void Move_to_next(){
// startActivity(new Intent(this, zzz.class));
}
private void createDialog(String title, String text){
// Création d'une popup affichant un message
AlertDialog ad = new AlertDialog.Builder(this).setPositiveButton("Ok", null).setTitle(title).setMessage(text).create();
ad.show();
}
} |
Partager