在“用户”表中,我将客户注册,并且要访问菜单,您必须登录,所以我需要验证客户端,使用 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');
?>