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
|
}
public class Login extends ActionBarActivity implements View.OnClickListener {
Button ok,back,exit;
TextView result;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.lbl_result);
}
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
HttpPost httppost = new HttpPost("http://localhost/Android/login.php");
HttpPost httppost2 = new HttpPost("http://localhost/Android/login2.php");
try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request"); //
HttpResponse response = httpclient.execute(httppost); // On execute la requete 1 qui permet la recuperation du nom
HttpResponse response2 = httpclient.execute(httppost2); //On execute la requete 2 qui permet la recuperation du mdp
String str2 = inputStreamToString(response2.getEntity().getContent()).toString(); // Conversion de la requete 2 en String
String str = inputStreamToString(response.getEntity().getContent()).toString(); // Conversion de la requete 1 en String
Log.w("SENCIDE", str);
if(username==str && password== str2) // Si le nom et le mdp taper par l'utilisateur est egale aux nom et mdp ds la bdd
{
Log.w("SENCIDE", "TRUE");
result.setText("Connexion reussie"); // affiche connexion reussie
}
else
{
Log.w("SENCIDE", "FALSE");
result.setText("Mdp ou login erroné");
}
} catch (ClientProtocolException | UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
public void onClick(View view) {
if(view == ok){
postLoginData();
} |
Partager