0

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);
0

1 Answer 1

1

Looks like you're printing the response twice in the PHP code.

Just remove the echo statements from the if/else conditions, and just print the response one time:

<?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; 
    //remove this: 
    //echo json_encode($response);  
} else {
    $response["success"] = false; 
    //remove this: 
    //echo json_encode($response);  
}

echo json_encode($response);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that a while back (and just tried it again) but the problem persists. It's still passing data to the server and updating the table but the same 'can't convert to JSONObject' error is showing. I'm not sure what's going wrong with it honestly
Well then just look at exactly what the server is sending back to you. Put a log statement at the top of the onResponse() callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.