我有一个简单的注册脚本,但是android studio无法正确将数据放入php脚本中。我在浏览器上运行了php脚本,它可以很好地放置数据,但是android studio却做得不好。
这是注册请求脚本。
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://192.168.*.*:80/phptesting/Register.php";
private Map<String, String> params;
public RegisterRequest(String username, String password,String isAdmin,
Response.Listener<String> listener,
Response.ErrorListener errListener){
super(Method.POST, REGISTER_REQUEST_URL,listener,errListener);
params = new HashMap<>();
params.put("username",username);
params.put("password",password);
params.put("isAdmin",isAdmin+"");
}
public Map<String, String> getparams() {
return params;
}
}
这是创建用户脚本
public class CreateUser extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_user);
this.setTitle("Create User");
final EditText username1 = findViewById(R.id.Createusername);
final EditText password1 = findViewById(R.id.CreatePassword);
final Switch isAdmin = findViewById(R.id.isadmin);
final Button createuser = findViewById(R.id.createuserbtn);
if (getIntent().hasExtra("com.example.northlandcaps.crisis_response")){
isAdmin.setVisibility(View.GONE);
}
createuser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = username1.getText().toString();
final String password = password1.getText().toString();
final String isadmin = isAdmin.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response Value: ", response);
if (response.equals("success")){
Intent intent = new Intent(CreateUser.this, MainActivity.class);
CreateUser.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry",null)
.create()
.show();
}
}
};Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_SHORT).show();
}
};
RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener,errorListener);
RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
queue.add(registerRequest);
}
});
}
这是php脚本
<?php
$db_host = '192.168.*.*:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
var_dump($_POST["username"]);//line 6
$con = mysqli_connect($db_host,'root',$db_pass,$db_name);
if($con){
echo "connection successful";
}
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if(isset($_POST["isAdmin"]) && isset($_POST["username"]) && isset($_POST["password"]))
{
$username = $_POST["username"];
$password = $_POST["password"];
$isAdmin = $_POST["isAdmin"];
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement,'ssi',$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(!$statement)
{
printf("Prepare failed: %s\n", mysqli_error($con));
}
echo "success";
}
else
echo "values not set";
?>
运行此命令时,应用程序会创建重试错误弹出消息。输出是这个;
注意:未定义索引:第6行的C:\ xampp \ htdocs \ phptesting \ Register.php中的用户名未设置NULL连接成功值
我相信它抱怨用户名没有任何意义。这是一个很大的问题,因为其他变量在下面定义,并且我不知道它们是否通过了脚本是否提前停止了? 感谢所有帮助,谢谢
答案 0 :(得分:0)
尝试在php文件中获取像这样的json
$json = json_decode(file_get_contents('php://input'), true);