意外的响应代码404 Volley Java Android

时间:2019-05-27 09:32:47

标签: java php mysql android-volley registration

当我尝试以注册表格添加新用户时,出现错误,并且没有要在users_table中添加的内容。当我尝试登录时也会出现此错误。

我将xampp用于phpmyadmin,运行MySQL,Apache和PHP。我尝试实现数据库的注册和登录,但出现错误404。请帮助解决此问题。

public class registration extends AppCompatActivity {

    private TextView loginReg;

    private EditText email, username, password;

    private Button signUp;

    private static String URL_REGIST = "http://XXX.XX.XX.X/android_register_login/register.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        loginReg = (TextView) findViewById(R.id.loginReg);
        email = findViewById(R.id.emailReg);
        username = findViewById(R.id.usernameReg);
        password = findViewById(R.id.passwordReg);
        signUp = findViewById(R.id.signUp);
        loginReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openLogin();
            }
        });
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Register();
            }
        });
    }
    public void openLogin(){
        Intent intent = new Intent(this, com.example.myfirstjavaapp.MainActivity.class);
        startActivity(intent);
    }
    private void Register(){
        final String email = this.email.getText().toString().trim();
        final String username = this.username.getText().toString().trim();
        final String password = this.password.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                    try{
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");

                        if(success.equals("1")){
                            Toast.makeText(registration.this,"Register success!", Toast.LENGTH_LONG).show();
                        }
                    }catch (JSONException e){
                        e.printStackTrace();
                        Toast.makeText(registration.this,"Register error!"+ e.toString(), Toast.LENGTH_LONG).show();
                    }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(registration.this,"Register error!"+ error.toString(), Toast.LENGTH_LONG).show();
                    }
                })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                params.put("email", email);
                params.put("password", password);
                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);



    }
}

这是PHP代码。

<?php

if ($_SERVER['REQUEST_METHOD'] =='POST'){

    $username = filter_input(INPUT_POST, 'username');
    $email = filter_input(INPUT_POST, 'email');
    $password = filter_input(INPUT_POST, 'password');

    $password = password_hash($password, PASSWORD_DEFAULT);

    require_once 'connect.php';

    $sql = "INSERT INTO users_table (username, email, password) VALUES ('$username', '$email', '$password')";

    if ( mysqli_query($conn, $sql) ) {
        $result["success"] = "1";
        $result["message"] = "success";

        echo json_encode($result);
        mysqli_close($conn);

    } else {

        $result["success"] = "0";
        $result["message"] = "error";

        echo json_encode($result);
        mysqli_close($conn);
    }
}

?>
  

E / Volley:[696] BasicNetwork.performRequest:http://XXX.XX.XX.X/android_register_login/register.php的意外响应代码404

0 个答案:

没有答案