如何在Android中远程匹配数据库中的名称和密码


How to match name and password from database remotely in Android

我想从PHP服务器上的数据库中获取用户数据,并将其保存到本地数据库上的按钮单击。我使用以下步骤:

  1. 检查用户是否连接到internet
  2. EditText的名称和密码发送到服务器

然后,从服务器端:

  1. 如果用户已注册,则发送结果0
  2. 如果用户未注册,则发送结果1
  3. 如果字段为空,则发送结果-1

我总是得到-1的结果,这表明我的字段是空的。但是,当我检查日志时,它不是空的。

MakeSong tsk1 = new MakeSong();
tsk1.execute(etName.getEditableText().toString(), etCountry.getEditableText().toString());
private class MakeSong extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {       
        //StringBuilder answer = new StringBuilder();
        String etmail = params[0];
        String etpass = params[1];
        Log.v("mail", etmail);
        Log.v("pass", etpass);
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/try/index.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("usermail", etmail));
            nameValuePairs.add(new BasicNameValuePair("userpass", etpass));
            httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httppost);
            Log.v("response", ""+response);
            InputStream inputStream = response.getEntity().getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder stringBuilder = new StringBuilder();
            String bufferedStrChunk = null;
            while((bufferedStrChunk = bufferedReader.readLine()) != null){
                stringBuilder.append(bufferedStrChunk);
            }
            return stringBuilder.toString();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return "hola";
    }
    @Override
    protected void onPostExecute(String result) {
        //do nothing
        //txtGen.setText(result);
        Log.v("GtSts", result);
    }
}

这是我用来检查用户验证的服务器交互的代码。我正在通过POST名称获取值,该名称未在Android EditText中定义。

我想知道的是:PHP POST中是否有任何方法只检查我发送的值,所以我只需要匹配那些?

<?php
$path="../";
include "../config.php";
$run=false;
$toret = array('result' => "",'uid' => "",'kid' => "",'user' => "",'master' =>"");
if(!empty($_POST['name']) && !empty($_POST['password'])  ) {
    $demail=$_POST['name'];
    $dpass=md5($_POST['password']);
    $mail=$bd->execute("user_info",
            "*","ui_email_id='".$demail."'");
    if(count($mail)>0) {
        $passwrd=$bd->execute("master",
                "*","m_password='".$dpass."'");        
        if(count($passwrd)>0) {                
            $toret = array('result' => "1",'uid' => $mail[0]['ui_userid'],'kid_info' =>$kid,'user_info' => $mail,'master' =>$passwrd);
        } else { 
            $toret["result"]= "0";
        }
    } else { 
        $toret["result"]= "0";
    }
} else {
    $toret["result"]= "-1";
}                
header("Content-Type: application/json");
echo json_encode($toret);
?>

使用Json数据返回您的结果,这是更合适的。

参考此。这正是你想要的:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/