0

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

1 回答 1

1

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

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

于 2012-04-12T14:46:18.330 回答