php使用JSON检查数据库中是否存在两个字段

时间:2012-04-12 14:29:05

标签: java php android json phpmyadmin

在'user'表中我把客户注册了,要访问菜单你必须登录所以我需要验证客户端,用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

&GT;

 <?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'); 
?>

1 个答案:

答案 0 :(得分:1)

首先,您在UI线程上使用网络。这是错的。使用AsyncTask来做到这一点。

然后,'它不起作用'不是一个非常有用的问题描述。你能开始连接吗?你有什么例外吗?你看到服务器上的连接了吗?你有连接中的一些数据吗?你收到答复了吗?答案是什么?