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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| 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.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
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 {
/** Called when the activity is first created. */
String name="",pass="";
EditText un,pw;
TextView error;
Button ok;
byte[] data;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
InputStream inputStream;
SharedPreferences app_preferences ;
List<NameValuePair> nameValuePairs;
private ProgressDialog progressDialog;
CheckBox check;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connexion_layout);
un=(EditText)findViewById(R.id.username);
pw=(EditText)findViewById(R.id.password);
ok=(Button)findViewById(R.id.connexion);
error=(TextView)findViewById(R.id.tv_error);
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
un = (EditText) findViewById(R.id.username);
pw = (EditText) findViewById(R.id.password);
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"))
{
un.setText(Str_user);
pw.setText(Str_pass);
check.setChecked(true);
}
ok.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start the progress dialog
progressDialog = ProgressDialog.show(ConnexionActivity.this, "", "Loading...");
name = un.getText().toString();
pass = pw.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, "Champs vides .. Veuillez entrer vos identifiants", Toast.LENGTH_LONG).show();
error.setText("Champs invalides !!!");
}
else
{
new Thread() {
public void run() {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", name));
postParameters.add(new BasicNameValuePair("password", pass));
//String valid = "1";
String response = null;
try {
//response = CustomHttpClient.executeHttpPost("http://127.0.0.1/ACMS_Project/connexion.php", postParameters);
response = CustomHttpClient.executeHttpPost("http://192.168.217.1/ACMS_Project/connexion.php", postParameters); //Enetr Your remote PHP,ASP, Servlet file link
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
if(res.equals("Y")) {
error.setText("Accès autorisé");
// dismiss the progress dialog
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Accès autorisé", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), DashbordActivity.class);
i.putExtra("selection", "causerie");
startActivity(i);
}
else{
// dismiss the progress dialog
progressDialog.dismiss();
error.setText("Nom utilisateur et/ou mot de passe incorrecte");
AlertDialog alertDialog = new AlertDialog.Builder(ConnexionActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Accès refusé");
// Setting Dialog Message
alertDialog.setMessage("Nom utilisateur et/ou mot de passe incorrecte");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.busy);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
} catch (Exception e) {
// dismiss the progress dialog
progressDialog.dismiss();
error.setText("Impossible de se connecter au serveur");
AlertDialog alertDialog = new AlertDialog.Builder(ConnexionActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Connexion impossible");
// Setting Dialog Message
alertDialog.setMessage("Impossible de se connecter au serveur; Vérifier vos paramètres de connexion");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.warning);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
}.start();
}
}
});
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();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.connexion, menu);
return true;
}
} |
Partager