Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

java - Android studio doesn't post to localhost, and doesn't give an error.

I have a basic registration script. It took me forever to get to this point, but I'm getting an "error." The app is supposed to send 3 strings of login registration to a localhost phpmyadmin. The app works fine, up to the point where it will generate a error popup message, which i programmed it to do. But it does't tell me why the registration has failed.

Here is the createsuser script. It is supposed to grab variables from the text boxes and send them over to a RegisterRequest script. It waits for a response, and either kicks the user back to the main menu or creates the retry message. it create an retry message, but no error to log.

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();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener);
                RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
                queue.add(registerRequest);
            }
        });
    }

Here is the registerrequest. It receives strings from CreateUser and posts them to the php script. I get no errors from this script, but there might be some

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){
        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;
    }
}

And here is the register php script.

<?php
    $db_host = '192.168.*.*:3306';
    $db_user = 'root';
    $db_pass = '';
    $db_name = 'test';

    $con = mysqli_connect($db_host,'root',$db_pass,$db_name);
    if($con){
        echo "connection successful";
    }
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
    }
    $isAdmin ="no";
     $username="fakename";
     $password="fakepassword";
    $isAdmin = isset($_POST["isAdmin"]);
    $username = isset($_POST["username"]);
    $password = isset($_POST["password"]);
    echo $isAdmin;
    echo $username;
    echo $password;
    $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
", mysqli_error($con)); }
    echo "success";
?>

I appreciate all help, thanks

(Someone told me to add a Var_dump to the php script, and work backwards. (thanks riggs), so what i got as a result is this: connection successfularray(0) { } success

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change

    $isAdmin ="no";
 $username="fakename";
 $password="fakepassword";
$isAdmin = isset($_POST["isAdmin"]);
$username = isset($_POST["username"]);
$password = isset($_POST["password"]);
echo $isAdmin;
echo $username;
echo $password;
$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
", mysqli_error($con)); }
echo "success";

To

if(isset($_POST["isAdmin"]) &&  isset($_POST["username"]) && isset($_POST["password"]))
{
    $username = isset($_POST["username"]);
    $password =  isset($_POST["password"]);
    $isAdmin = isset($_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
", mysqli_error($con)); 
    }

    echo "success";
}
else
 echo "values not set";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...