Reputation: 942
I'm still a newbie in android apps development and have had this problem for a long time. I'm trying to create a register page, but it the post data is not sent. This is my code:
RegisterActivity
public class RegisterActivity extends AppCompatActivity {
private EditText name, email, password, c_password;
private Button register_button;
private ProgressBar loading;
private static String URL_REGIST = "http://192.168.1.6:7777/androidregistertest/register.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
loading = findViewById(R.id.loading);
name = findViewById(R.id.name);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
c_password = findViewById(R.id.c_password);
register_button = findViewById(R.id.register_button);
register_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Regist();
}
});
}
private void Regist() {
loading.setVisibility(View.VISIBLE);
register_button.setVisibility(View.GONE);
final String name = this.name.getText().toString().trim();
final String email = this.email.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(RegisterActivity.this, "Register Success", Toast.LENGTH_SHORT).show();
}
}
catch (JSONException e){
e.printStackTrace();
Toast.makeText(RegisterActivity.this, "Register Error! " + e.toString(), Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
register_button.setVisibility(View.VISIBLE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RegisterActivity.this, "Register Error 2! " + error.toString(), Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
register_button.setVisibility(View.VISIBLE);
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return super.getParams();
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
register.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$password = password_hash($password, PASSWORD_DEFAULT);
require_once 'connect.php';
$sql = "INSERT INTO logins (name, email, password) VALUES ('".$name."', '".$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";
}
}
When I click the register button, the response contain following error message,
Notice: Undefined index: name in D:\xampp-7\htdocs\androidregistertest\register.php on line 3
Notice: Undefined index: email in D:\xampp-7\htdocs\androidregistertest\register.php on line 4
Notice: Undefined index: password in D:\xampp-7\htdocs\androidregistertest\register.php on line 5
Why post data are not sent?
Upvotes: 1
Views: 68
Reputation: 460
Everything is seems to be correct except one thing, that you have added param to Map but does not return it to volly for sending it. Your method need a single line to change
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return super.getParams(); //Change this
return params; //to this
}
Best Wishes.
Upvotes: 0