Créer une page de login pour une application android

ingndjana Messages postés 3 Date d'inscription samedi 31 mai 2014 Statut Membre Dernière intervention 2 juin 2014 - Modifié par jordane45 le 1/06/2014 à 02:07
ingndjana Messages postés 3 Date d'inscription samedi 31 mai 2014 Statut Membre Dernière intervention 2 juin 2014 - 2 juin 2014 à 00:42
bonjour à tous,

je voudrais créer une appli sur android où l'utilisateur devra d'abord s'identifier avant d'avoir accès à l'appli et je suis bloqué au niveau de la page de login

l'appli doit se connecter à une base de données mysql pour vérifier via un script php si les identifiants entrés sont bien correctes et ouvrir la page d'accueil si oui, afficher une boite de dialogue si non

j'ai parcouru plusieurs tutos mais sans succès

voici mon code:

script php :

<?php
 
$hostname_localhost ="localhost";
$database_localhost ="acms_db";
$username_localhost ="root";
$password_localhost ="";
 
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
 
mysql_select_db($database_localhost,$localhost);
 
$useremail = $_POST['UserEmail'];
$password = $_POST['Password'];
 
 $query_search = "select * from utilisateur where username = '".$useremail."' AND password = '".$password. "'";
 $query_exec = mysql_query($query_search) or die(mysql_error());
 $rows = mysql_num_rows($query_exec);
 
 if($rows --> 0) 
 { 
 echo "Y"; 
}else  {
 echo "N"; 
}
?>

code Java :
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();
 
 }
}

la réponse à la requête : (buffer.charAt(0)) est supposée être un "Y" ou un "N" mais il s'agit plutôt du symbole "<", En vérifiant je me sui rendu compte que la réponse est du html du genre <!DOCTYPE ...,

je ne sais pas à quel niveau il y a problème

Je voudrais également afficher une progressDialog pendant le traitement de la requête juste après le click sur le bouton "login" mais il ne s'affiche pas.

Quelqu'un pourrait-il m'aider ?
merci

4 réponses

jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
1 juin 2014 à 02:08
Bonjour,
J'ai édité ton message pour y mettre la coloration syntaxique.
Merci d'y penser à l'avenir :
utilisation de la coloration syntaxique :
Utilisation des Balises de code
0
jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
1 juin 2014 à 02:09
Pour ce qui est de ton souci..
Je pense qu'il faut que tu t'orientes vers le XML.

=> Tu encodes en XML (dans ton script PHP) le résultat retourné

=> Tu décodes (tu parses) le XML dans ton script JAVA pour lire la donnée.


0
ingndjana Messages postés 3 Date d'inscription samedi 31 mai 2014 Statut Membre Dernière intervention 2 juin 2014
1 juin 2014 à 23:58
Merci de ta réponse,

mais aurai tu un exemple ?
0
jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
2 juin 2014 à 00:26
0
ingndjana Messages postés 3 Date d'inscription samedi 31 mai 2014 Statut Membre Dernière intervention 2 juin 2014
2 juin 2014 à 00:42
ok merci j'assai
0
Rejoignez-nous