我的php脚本已连接,但android studio不会使用它。没有可见的错误输出

时间:2018-11-02 23:41:33

标签: java php android mysql android-studio

我有一个用于处理非常基本的注册的php脚本。它在我的本地主机的文件夹中; http://(ipaddress/localhost):3306/testing/Register.php。 Android Studio指向该URL,但是运行模拟器时没有错误输出。当我在浏览器中打开PHP脚本时,一切看起来都很好。

这是registerrequest Java脚本:

public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://(ipaddress/localhost):3306/testing/Register.php";
    private Map<String, String> params;
    public RegisterRequest(String username, String password,String isAdmin, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL,listener,null);
        params = new HashMap<>();
        params.put("username",username);
        params.put("password",password);
        params.put("isAdmin",isAdmin+"");
    }

    public Map<String, String> getparams() {
        return params;
    }
}

这是我的createuser脚本

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) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success){
                                Intent intent = new Intent(CreateUser.this, MainActivity.class);
                                startActivity(intent);
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry",null)
                                        .create()
                                        .show();


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener);
                RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
                queue.add(registerRequest);
            }
        });
    }

最后,这是我的PHP脚本,Register;米

$db_host = 'localhost:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';

$con = mysqli_connect($db_host,'user',$db_pass,$db_name);
if($con){
    echo "connection successful";
}else{
    echo "connection failed";
}

$age = $_POST["isAdmin"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
if(!$statement) { printf("Prepare failed: %s\n", mysqli_error($con)); }
if(!$statement) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }
mysqli_stmt_bind_param($statement, "ssi",$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(mysqli_error($statement)) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }

$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

我正在使用xampp(已打开Apache和mysql),我的寄存器位于Htdocs中的文件夹中。 预先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以修改int的构造函数:

RegisterRequest

public RegisterRequest(String username, String password,String isAdmin, Response.Listener<String> listener, Response.ErrorListener() errListener){ //add error listener super(Method.POST, REGISTER_REQUEST_URL,listener,errListener); ...... } 类文件中,添加以下内容:

CreateUser

然后:

Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, String.valueOf(error), Toast.LENGTH_SHORT).show();
        }
}

现在,再次运行,当发生错误时,您可以在错误侦听器中看到它。