我想从PHP服务器上的数据库中获取用户数据,并在点击按钮时将其保存到本地数据库。我正在使用以下步骤:
EditText
获取名称和密码并将其发送到服务器然后,从服务器端:
我总是在结果中得到-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名称未在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);
?>
答案 0 :(得分:0)
使用Json数据返回结果更合适。
参考这个。它完全符合您的要求:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/