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 210 211 212 213 214 215 216
| private void doLogin(final String login, final String pass)
{
final String pw = md5(pass);
// Création d'un thread
Thread t = new Thread()
{
public void run()
{
Looper.prepare();
// On se connecte au serveur afin de communiquer avec le PHP
DefaultHttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpResponse response;
HttpEntity entity;
try
{
// On établit un lien avec le script PHP
HttpPost post = new HttpPost(UPDATE_URL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", login));
nvps.add(new BasicNameValuePair("password", pw));
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
// On passe les paramètres login et password qui vont être récupérés
// par le script PHP en post
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
// On récupère le résultat du script
response = client.execute(post);
entity = response.getEntity();
InputStream is = entity.getContent();
// On appelle une fonction définie plus bas pour traduire la réponse
read(is);
is.close();
if (entity != null)
entity.consumeContent();
}
catch (Exception e)
{
progressDialog.dismiss();
createDialog("Error", "Couldn't establish a connection");
}
Looper.loop();
}
};
t.start();
}
private void read(InputStream in)
{
// On traduit le résultat d'un flux
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp;
try
{
sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// Cette classe est définie plus bas
LoginContentHandler uch = new LoginContentHandler();
xr.setContentHandler(uch);
xr.parse(new InputSource(in));
}
catch (ParserConfigurationException e)
{
}
catch (SAXException e)
{
}
catch (IOException e)
{
}
}
private String md5(String in)
{
MessageDigest digest;
try
{
digest = MessageDigest.getInstance("MD5");
digest.reset();
digest.update(in.getBytes());
byte[] a = digest.digest();
int len = a.length;
StringBuilder sb = new StringBuilder(len << 1);
for (int i = 0; i < len; i++)
{
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return null;
}
private class LoginContentHandler extends DefaultHandler
{
// Classe traitant le message de retour du script PHP
private boolean in_loginTag = false;
private int userID;
private boolean error_occured = false;
public void startElement(String n, String l, String q, Attributes a)
throws SAXException
{
if (l == "login")
in_loginTag = true;
if (l == "error")
{
progressDialog.dismiss();
switch (Integer.parseInt(a.getValue("value"))
{
case 1:
createDialog("Error", "Couldn't connect to Database");
break;
case 2:
createDialog("Error", "Error in Database: Table missing");
break;
case 3:
createDialog("Error", "Invalid username and/or password");
break;
}
error_occured = true;
}
if (l == "user" && in_loginTag && a.getValue("id") != "")
// Dans le cas où tout se passe bien on récupère l'ID de l'utilisateur
userID = Integer.parseInt(a.getValue("id"));
}
public void endElement(String n, String l, String q) throws SAXException
{
// on renvoie l'id si tout est ok
if (l == "login")
{
in_loginTag = false;
if (!error_occured)
{
progressDialog.dismiss();
Intent i = new Intent();
i.putExtra("userid", userID);
quit(true, i);
}
}
}
public void characters(char ch[], int start, int length)
{
}
public void startDocument() throws SAXException
{
}
public void endDocument() throws SAXException
{
}
}
} |
Partager