「ユーザー」テーブルに顧客を登録しました。メニューにアクセスするにはログインする必要があるため、php と Json を使用してクライアントを確認する必要があります。しかし、それは機能しません
これは l'activité loginActivity です
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final EditText txtNumCompte = (EditText)findViewById(R.id.txtNumCompte);
final EditText txtCodCompte = (EditText)findViewById(R.id.txtCodCompte);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String numcompte = txtNumCompte.getText().toString();
String codecompte = txtCodCompte.getText().toString();
if (numcompte.equals("") || codecompte.equals(""))
{
Toast.makeText(getBaseContext(),
"Numéro et code confidentiel de compte sont obligatoires",
Toast.LENGTH_SHORT).show();
return;
}
Pattern p = Pattern.compile(".[0-9]+");
Matcher m = p.matcher(numcompte);
if (m.matches() == false)
{
Toast.makeText(LoginActivity.this,
"La carte est non trouvée \nVeuillez vérifier son numéro",
Toast.LENGTH_SHORT).show();
return;
}
try{
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpParams d = new BasicHttpParams();
d.setParameter("user", "1");
HttpClient httpclient = new DefaultHttpClient(d);
String url = "http://10.0.2.2/connect.php";
HttpPost httppost = new HttpPost(url);
try {
Log.i(getClass().getSimpleName(), "send task - start");
//
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,
responseHandler);
JSONObject json = new JSONObject(responseBody);
JSONArray jArray = json.getJSONArray("posts");
for (int i = 0; i < jArray.length(); i++) {
JSONObject e = jArray.getJSONObject(i);
String s = e.getString("post");
if(s!=null) a=a+1;
}
if(a==0)
{
Toast.makeText(LoginActivity.this,"Connecté avec succès", Toast.LENGTH_LONG).show();
Intent intent= new Intent(LoginActivity.this,MenuActivity.class);
startActivity(intent);
}
else{
Toast.makeText(LoginActivity.this,"Numéro/code confidentiel invalide", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Toast.makeText(LoginActivity.this,"dsl , probleme de connexion",Toast.LENGTH_LONG).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (JSONException e) {
Toast.makeText(LoginActivity.this,"dsl , probleme dans le serveur , essayez autre fois",Toast.LENGTH_LONG).show();
}
}
});
}
}
これはconnct.phpです
>
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
$con = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
mysql_select_db('baseandroid',$con);
$sql = 'SELECT count(*) FROM user WHERE Num_Compte=".$obj->{'num'}.'" AND Code_Compte="'.$obj->{'code'}.'"';
$req = mysql_query($sql) ;
$data = mysql_fetch_array($req);
mysql_free_result($req);
$query = "SELECT * FROM Compte where Num_Compte=".$obj->{'num'}."";
$result = mysql_query($query,$con) ;
/* create one master array of the records */
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
@mysql_close($con);
header('Content-type: application/json');
?>