0

I need to send 5 strings and 3 integers as parameters to my server. I keep receiving this error back: "Required parameters (name, email, password, gender or dob) is missing!" [found at the end of my php code]. How do I send multiple primitive types as parameters to my server? Prior to messing around with this code, I was using a StringRequest and a HashMap as a return type for the overridden method: getParams(). I have now added the integers...dob_day, dob_month, and dob_year and have run into trouble. Here is some code...

private void registerUser(final String first_name,  final String last_name, final String email, final String password, final String gender, final int dob_day, final int dob_month, final int dob_year) {

        // Tag used to cancel the request
        String cancel_req_tag = "register";

        progressDialog.setMessage("Verifying Registration...");
        showDialog();

        JSONObject jObj = new JSONObject();
        try {
            jObj.put("first_name", first_name);
            jObj.put("last_name", last_name);
            jObj.put("email", email);
            jObj.put("password", password);
            jObj.put("gender", gender);
            jObj.put("dob_day", dob_day);
            jObj.put("dob_month", dob_month);
            jObj.put("dob_year", dob_year);
            Log.i("jsonString", jObj.toString());
        } catch(Exception e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST,
                URL_FOR_REGISTRATION, jObj, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, "Register Response: " + response.toString());
                hideDialog();

                try {
                    boolean error = response.getBoolean("error");

                    if (!error) {
                        String user = response.getJSONObject("user").getString("first_name");
                        Toast.makeText(getApplicationContext(), "Registration Successful! \nWelcome, " + user +"!", Toast.LENGTH_LONG).show();

                        SharedPreferences preferences = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString(PREF_USERNAME, signupInputEmail.getText().toString());
                        editor.apply();

                        // Launch login activity
                        Intent intent = new Intent(
                                RegisterActivity.this,
                                LoginActivity.class);
                        startActivity(intent);
                        finish();
                    } else {

                        String errorMsg = response.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            /**
             * Passing some request headers
             */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        // Adding request to request queue
        AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonReq, cancel_req_tag);
    }
}

SERVER-SIDE:

<?php

require_once 'update_user_info.php';
$db = new update_user_info();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['gender']) && isset($_POST['dob_day']) && isset($_POST['dob_month']) && isset($_POST['dob_year'])) {

    // receiving the post params
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $gender = $_POST['gender'];
    $dob_day = $_POST['dob_day'];
    $dob_month = $_POST['dob_month'];
    $dob_year = $_POST['dob_year'];

    // check if user is already existed with the same email
    if ($db->CheckExistingUser($email)) {
        // user already existed
        $response["error"] = TRUE;
        $response["error_msg"] = "User already exists with email:" . $email;
        echo json_encode($response);
    } else {
        // create a new user
        $user = $db->RegisterUser($first_name, $last_name, $email, $password, $gender, $dob_day, $dob_month, $dob_year);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["user"]["first_name"] = $user["first_name"];
            $response["user"]["last_name"] = $user["last_name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["gender"] = $user["gender"];
            $response["user"]["dob_day"] = $user["dob_day"];
            $response["user"]["dob_month"] = $user["dob_month"];
            $response["user"]["dob_year"] = $user["dob_year"];
            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Registration Error!";
            echo json_encode($response);
        }
}
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters (name, email, password, gender or dob) is missing!";
    echo json_encode($response);
}
?>
Thientvse
  • 1,753
  • 1
  • 14
  • 23

1 Answers1

1

You are sending a json text to your php script.

But your php script expects POST parameters.

Incompatible setup.

You have two options to make them compatible.

-You can change your php script to receive json text.

-You can change the sending side to send post parameters in the standard way.

greenapps
  • 11,154
  • 2
  • 16
  • 19