我已成功使用httpsost从java Android代码通过php到mysql创建身份验证。但是我遇到了创建新用户的问题。我检查了其他用户的多个帖子,我觉得它看起来不错。此外,我已经创建了一个简单的html表单来测试新用户的php创建,这也是有效的。
任何想法都将不胜感激。这是我的java代码和PHP下面的代码。谢谢!
try {
// create new array list
nameValuePairs = new ArrayList<NameValuePair>();
// place them in an array list
nameValuePairs.add(new BasicNameValuePair("username", user));
nameValuePairs.add(new BasicNameValuePair("password", pass));
// add array list to http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// assign executed form container to response
response = httpclient.execute(httppost);
// check status code,
if (response.getStatusLine().getStatusCode() == 200) {
// assign response entity to htpp entitiy
entity = response.getEntity();
// chekc if entity is not null
if (entity != null) {
Log.i("RESPONSE",EntityUtils.toString(entity));
Intent intent = new Intent(CreateAccount.this,
MainActivity.class);
startActivity(intent);
// }
// display a taost saying login was a success
Toast.makeText(CreateAccount.this, user,
Toast.LENGTH_SHORT).show();
// }
}
}
} catch (Exception e) {
e.printStackTrace();
// display toast when there is a connection error
Toast.makeText(getBaseContext(), "ConnectionError",
Toast.LENGTH_SHORT).show();
}
PHP代码:
//get form data
$user = mysql_real_escape_string($_POST['user']);
$pass = mysql_real_escape_string($_POST['pass']);
echo "INSERT INTO androidlogin (user, pass) VALUES ('$user','$pass')";
//insert data
$insert = mysql_query("INSERT INTO androidlogin (user, pass) VALUES ('$user','$pass')");
if($insert){
$arr2 = array("user" => $user, "pass" => $pass);
echo json_encode($arr2);
}
答案 0 :(得分:0)
当发布值称为“用户名”和“密码”时,服务器会检查“用户”和“用户”。 “通过”。你必须使用相同的密钥。