A section of my app works to update an online user database to change their name, and the php code regarding the 'UPDATE' function seems to be working properly, with the names changing in the database. However, part of the code that's meant to pass a response back to the app so that it can do other stuff isn't working properly, so the app isn't carrying out a bunch of important actions.
I'm using Volley for the server communication, if that helps.
I need to make it so that once the php file successfully updates a name field within the table (which it's already doing) it correctly passes back a response to the app, so that I can execute some commands.
Here's the activity with the interface etc.:
public class PopupNameChange extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_up_change_name);
final Button nameChangeButton = (Button) findViewById(R.id.confirmButton);
final EditText newName = (EditText) findViewById(R.id.etNewName);
final String userNameFromPref = UserCredentials.getLoggedInUserName(getApplicationContext());
final String userEmailFromPref = UserCredentials.getLoggedInEmailUser(getApplicationContext());
TextView userNameCurrent = (TextView) findViewById(R.id.tvCurrentName);
userNameCurrent.setText(userNameFromPref);
getWindow().setLayout(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
nameChangeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = newName.getText().toString();
final String email = userEmailFromPref.toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println("Print test");
*** the logcat error points at the line below ***
JSONObject jsonResponse = new JSONObject(response);
System.out.println(jsonResponse);
boolean success = jsonResponse.getBoolean("success");
if (success) {
UserCredentials.setLoggedInUserName(getApplicationContext(), name);
AlertDialog.Builder alertMessage = new AlertDialog.Builder(PopupNameChange.this);
alertMessage.setMessage("Name changed to: " + name)
.setNegativeButton("Close", null)
.create()
.show();
} else {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(PopupNameChange.this);
alertMessage.setMessage("Name change failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
NameChangeRequest nameChangeRequest = new NameChangeRequest(email, name, responseListener);
RequestQueue queue = Volley.newRequestQueue(PopupNameChange.this);
queue.add(nameChangeRequest);
}
});
}
Name Changing request class:
public class NameChangeRequest extends StringRequest{
private static final String NameChange_Request_URL = "http://app-user-base.000webhostapp.com/NameChange.php";
private Map<String, String> params;
public NameChangeRequest(String email, String name, Response.Listener<String> listener) {
super(Request.Method.POST, NameChange_Request_URL, listener, null);
params = new HashMap<>();
params.put("email", email);
params.put("name", name);
}
@Override
public Map<String, String> getParams() {
return params;
}
php file, stored on the database server (online):
<?php
$con = mysqli_connect("localhost", "dbUsername", "dbPassword", "dbName");
$email = $_POST["email"];
$name = $_POST["name"];
$statement = "UPDATE users SET name = '$name' WHERE email = '$email'";
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
$response = array();
$response["success"] = false;
if(mysqli_query($con,$statement)) {
$response["success"] = true;
echo json_encode($response);
} else {
$response["success"] = false;
echo json_encode($response);
}
echo json_encode($response);