值无法发送到数据库

时间:2017-12-02 18:13:02

标签: android json phpmyadmin radio-button

对于我的应用程序,在注册时,用户必须使用单选按钮(RadioGroup)

选择性别

在他们选择并填写注册表后,包括性别在内的所有数据都应存入phpmyAdmin。

我必须将RadioButton的值存储为字符串。

不幸的是,所有数据都没有被发送,甚至在我点击发送按钮后也没有发生任何事情。

    package com.example.prime.try;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.sql.Struct;


public class Register extends AppCompatActivity {

    private RadioGroup radioGroupGender;
    private RadioButton radioButtonGender;
    private String userGender = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        final EditText etName = (EditText) findViewById(R.id.etName);
        final EditText etAge = (EditText) findViewById(R.id.etAge);
        final TextView tvGender = (TextView) findViewById(R.id.tvGender);
        final EditText etNationality = (EditText) findViewById(R.id.etNationality);
        final EditText etEmail = (EditText) findViewById(R.id.etEmail);
        final RadioGroup radioGroupGender = (RadioGroup) findViewById(R.id.radioGroupGender);
        final RadioButton rbMale = (RadioButton) findViewById(R.id.rbMale);
        final RadioButton rbFemale = (RadioButton) findViewById(R.id.rbFemale);
        final Button bSend = (Button) findViewById(R.id.bSend);


        final int selectedId = radioGroupGender.getCheckedRadioButtonId();
        radioButtonGender = (RadioButton) findViewById(selectedId);
        //final String userGender = Integer.toString(radioButtonGender.getId());
        //final String userGender = radioButtonGender.getText().toString();
        //final String userGender = String.valueOf(radioButtonGender.getId());

        //final String userGender = {((RadioButton) findViewById(radioGroupGender.getCheckedRadioButtonId())).getText().toString()};


        bSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = etName.getText().toString();
                final int age = Integer.parseInt(etAge.getText().toString());
                final String nationality = etNationality.getText().toString();
                final String email = etEmail.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(getApplicationContext(), MainActivity.class);
                                startActivity(intent);
                                //Intent intent = new Intent(Register.this, MainActivity.class);
                                //Register.this.startActivity(intent);


                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };


                radioGroupGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged (RadioGroup radioGroupGender,int checkedId){
                        int id = radioGroupGender.getCheckedRadioButtonId();
                        RadioButton rb = (RadioButton) findViewById(id);

                        String userGender = rb.getText().toString();

                    }
                });



                RegisterRequest registerRequest = new RegisterRequest(name, age, userGender, nationality, email, responseListener);
                RequestQueue queue = Volley.newRequestQueue(Register.this);
                queue.add(registerRequest);
            }
        });
    }


    public String RadioButtonClicked(View view) {

        String userGender = "";
        boolean checked = ((RadioButton) view).isChecked();

        switch (view.getId()) {
            case R.id.rbFemale:
                if (checked)
                    userGender = "Female";
                break;
            case R.id.rbMale:
                if (checked)
                    userGender = "Male";
                break;
        }
        return userGender;
    }

}

这是php文件,感谢您提前的帮助:)

    <?php
    $con = mysqli_connect("bio.000webhostapp.com", "anonymous12345", "animals89", "testing1235");

    $name = $_POST["name"];
    $age = $_POST["age"];
    $Gend = $_POST["userGender"];
    $nationality = $_POST["nationality"];
    $email = $_POST["email"];


    $statement = mysqli_prepare($con, "INSERT INTO user (name, age, Gend, nationality, email) VALUES (?, ?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sssss", $name, $age, $Gend, $nationality, $email);
    mysqli_stmt_execute($statement);

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

    echo json_encode($response);


?>

0 个答案:

没有答案