Envoi login et mot de passe dans une URL
Bojour a tous,
je veux envoyé un login et mot de passe d'une application android à une page php. il faut que la page php vérifi l'existance de login et mot de passe.
ESt ce que vous pouvez m'aider.
voila le code php et les classe du projet android
Merci :)
Andr.php
----------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <?php
$login = $_REQUEST["login"] ;
$motdepass = $_REQUEST["motdepasse"] ;
$connexion=mysql_connect( "localhost" , "root" , "" );
mysql_select_db("servervideo");
$requete="SELECT * FROM client WHERE login = '".$login."' AND motdepasse = '".$motdepass."'";
$resultat=mysql_query($requete)or die(mysql_error());
$message=mysql_fetch_array($resultat);
?>
<?php
echo $message['login'];
?> |
---------------LoginActivity------------------
Code:
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
| package login.sample;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "Login";
Button signin;
String loginmessage = null;
Thread t;
private SharedPreferences mPreferences;
ProgressDialog dialog;
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = null;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {
}
return insrc;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String loginmsg=(String)msg.obj;
if(loginmsg.equals("SUCCESS")) {
removeDialog(0);
Intent intent=new Intent(getApplicationContext(),Welcome.class);
startActivity(intent);
finish();
}
}
};
public void tryLogin() {
Log.v(TAG, "Trying to Login");
EditText etxt_user = (EditText) findViewById(R.id.txt_username);
EditText etxt_pass = (EditText) findViewById(R.id.txt_password);
String username = etxt_user.getText().toString();
String password = etxt_pass.getText().toString();
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1/Serveur/Andr.php");
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("password", password));
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
Log.v(TAG, response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
Log.v(TAG, "Set response to responseEntity");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
LoginHandler myLoginHandler = new LoginHandler();
xr.setContentHandler(myLoginHandler);
xr.parse(retrieveInputStream(responseEntity));
ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) {
// Store the username and password in SharedPreferences after the successful login
SharedPreferences.Editor editor=mPreferences.edit();
editor.putString("UserName", username);
editor.putString("PassWord", password);
editor.commit();
Message myMessage=new Message();
myMessage.obj="SUCCESS";
handler.sendMessage(myMessage);
} else if(parsedLoginDataSet.getExtractedString().equals("ERROR")) {
Intent intent = new Intent(getApplicationContext(), LoginError.class);
intent.putExtra("LoginMessage", parsedLoginDataSet.getMessage());
startActivity(intent);
removeDialog(0);
}
} catch (Exception e)
{
Intent intent = new Intent(getApplicationContext(), LoginError.class);
intent.putExtra("LoginMessage", "Unable to login");
startActivity(intent);
removeDialog(0);
}
}
//Checking whether the username and password has stored already or not
private final boolean checkLoginInfo() {
boolean username_set = mPreferences.contains("UserName");
boolean password_set = mPreferences.contains("PassWord");
if ( username_set || password_set ) {
return true;
}
return false;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0: {
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait while connecting...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
if (!checkLoginInfo()) {
signin = (Button) findViewById(R.id.btn_sign_in);
signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(0);
t=new Thread() {
public void run() {
tryLogin();
}
};
t.start();
}
});
}
else {
/*Directly opens the Welcome page, if the username and password is already available
in the SharedPreferences*/
Intent intent=new Intent(getApplicationContext(),Welcome.class);
startActivity(intent);
finish();
}
}
} |
---------------Loginerror.java---------------
Code:
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
| package login.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LoginError extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.gc();
setContentView(R.layout.loginerror);
TextView textview = (TextView) findViewById(R.id.tv);
String loginMessage = getIntent().getStringExtra("LoginMessage");
textview.setText(loginMessage);
button = (Button) findViewById(R.id.btn_ok);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
} |
------------------LoginHander------------------
Code:
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
| package login.sample;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class LoginHandler extends DefaultHandler {
private boolean login = false;
private boolean status = false;
private boolean message = false;
private ParsedLoginDataSet myParsedLoginDataSet = new ParsedLoginDataSet();
public ParsedLoginDataSet getParsedLoginData() {
return this.myParsedLoginDataSet;
}
@Override
public void startDocument() throws SAXException {
this.myParsedLoginDataSet = new ParsedLoginDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("login")) {
this.login = true;
} else if (localName.equals("status")) {
this.status = true;
} else if (localName.equals("message")) {
this.message = true;
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if (localName.equals("login"))
this.login = false;
else if (localName.equals("status"))
this.status = false;
else if (localName.equals("message"))
this.message = false;
}
@Override
public void characters(char ch[], int start, int length) {
if (this.status) {
myParsedLoginDataSet.setExtractedString(new String(ch, start,length));
} else if (this.message) {
myParsedLoginDataSet.setMessage(new String(ch, start, length));
}
}
} |
-------------ParsedLoginDataSet---------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package login.sample;
public class ParsedLoginDataSet {
private String login = null;
private String message = null;
public String getExtractedString() {
return login;
}
public void setExtractedString(String extractedString) {
this.login = extractedString;
}
public void setMessage(String extractedString) {
this.message = extractedString;
}
public String getMessage() {
return this.message;
}
} |
-------------welcom-----------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package login.sample;
import android.app.Activity;
import android.os.Bundle;
public class Welcome extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.gc();
setContentView(R.layout.welcome);
}
} |