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

时间:2012-04-13 20:18:29

标签: php android json

使用php的技术Json,我创建了内容为jObject:JSONObject的“mylist”列表,我想测试这个列表是否为空(mylist.size())。 但它不起作用,因为它给了我permission denied错误。

登录活动是:

> public class LoginActivity extends Activity {
    int TIMEOUT_MILLISEC = 10000; // = 10 seconds


    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();

                }
                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();

                }
                try{

                    JSONObject json = new JSONObject();
                    json.put("num", txtNumCompte.getText().toString());
                    json.put("code", txtCodCompte.getText().toString());



                    HttpParams httpParams = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(httpParams,
                            TIMEOUT_MILLISEC);
                    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
                    HttpClient httpclient = new DefaultHttpClient(httpParams);
                    String url = "http://10.0.2.2/connect.php";
                    HttpPost httppost = new HttpPost(url);

                    try {
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();
                        String responseBody = httpclient.execute(httppost,
                                responseHandler);
                            JSONObject json2 = new JSONObject(responseBody);
                        JSONArray jArray = json2.getJSONArray("posts");
                        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

                         for (int i = 0; i < jArray.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            JSONObject e = jArray.getJSONObject(i);
                            String s = e.getString("post");
                            JSONObject jObject = new JSONObject(s);

                            map.put("numcompte", jObject.getString("Num_Compte"));
                            map.put("codecompte", jObject.getString("Code_Compte"));



                            mylist.add(map);}
                            if (mylist.size()==0){
                                Toast.makeText(LoginActivity.this,"Numéro/code confidentiel invalide", Toast.LENGTH_LONG).show();
                            }


                            else
                     {
                        Toast.makeText(LoginActivity.this,"Connecté avec succès", Toast.LENGTH_LONG).show();
                            Intent intent= new Intent(LoginActivity.this,MenuActivity.class);

                            startActivity(intent);

                }


                                } catch (ClientProtocolException e) {
                        Toast.makeText(LoginActivity.this,"dsl , probleme de connexion",Toast.LENGTH_LONG).show();
                        } catch (UnsupportedEncodingException e) {
                                        Toast.makeText(LoginActivity.this,"dsl", Toast.LENGTH_LONG).show();

                                } catch (IOException e) {
                                    Toast.makeText(LoginActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();

                        } 
                    }
                catch (JSONException e) {
                        Toast.makeText(LoginActivity.this,"dsl , probleme dans le serveur , essayez autre fois",Toast.LENGTH_LONG).show();

                        } 

            }
            });


    }
        }

这是connect.php

<?php 
$json = file_get_contents('php://input');
$obj = json_decode($json);

  $link = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
  mysql_select_db('baseposte',$link) or die('Cannot select the DB');

   $query = "SELECT * FROM user where '".$obj->{'num'}."'=Num_Compte and  '".$obj->{'code'}."'=Code_Compte";
  $result = mysql_query($query,$link) ;
 /* create one master array of the records */
  $posts = array();
  if(mysql_num_rows($result)) {
    while($post = mysql_fetch_assoc($result)) {
      $posts[] = array('post'=>$post);
    }
  }

    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));


  /* disconnect from the db */
  @mysql_close($link);
?>

0 个答案:

没有答案