Skip to content
Advertisement

how to get value inside JsonObject

im trying this new method Ion. with PDO and I got stuck where I want to get value inside JsonObject. I previously used Volly with msqli but I find Ion. more organized and PDO more safe.

whenever I try to add any code to java its just break and don’t work as if no connection same with PHP so I got stuck

part where I’m stuck at is LOG_IN_SUCCESS as I need to get user id for session manager as $row doesn’t show in java only the case “LOG_IN_SUCCESS” i also added case”LOG_IN_SUCCESS “: case”0”:{code break;} but i didnt work

I tried to add new string for case”0″which contain user Id but code just break

java

      private void loginbtn(final String email, final String password) {


    Ion.with(getApplicationContext())
            .load(HOST)
            .setBodyParameter("susers_semails", email)
            .setBodyParameter("susers_spasswords", password)
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {

                    try {

                        String completed = result.get("LOGIN").getAsString();

                        switch (completed){

                            case "Email_Doesnt_Exist":
                                edtemail.setError("Email Doesnt Exist");
                                loginbtn.setVisibility(View.VISIBLE);
                                progressBar.setVisibility(View.GONE);
                                break;

                            case "Log_In_Success":


                                    Intent intent = new Intent(LoginActivity.this, HomeSearchActivity.class);
                                    startActivity(intent);
                                    finish();
                                    loginbtn.setVisibility(View.VISIBLE);
                                    progressBar.setVisibility(View.GONE);
                                    Toast.makeText(LoginActivity.this, completed, Toast.LENGTH_LONG);
                                    Log.d(TAG, "onCompleted: " + completed);
                                break;





                            case "Log_In_Failed":
                                edtpassword.setError("Incorrect Password");
                                loginbtn.setVisibility(View.VISIBLE);
                                progressBar.setVisibility(View.GONE);

                                break;

                            default:
                                Toast.makeText(getApplicationContext(), "Ops! Error Occurred m", Toast.LENGTH_LONG).show();
                                loginbtn.setVisibility(View.VISIBLE);
                                progressBar.setVisibility(View.GONE);

                                break;

                        }
                    }catch (Exception error){
                        Toast.makeText(getApplicationContext(), "Please Check Connection And Try Again", Toast.LENGTH_LONG).show();
                        loginbtn.setVisibility(View.VISIBLE);
                        progressBar.setVisibility(View.GONE);


                    }

                }
            });


}

php

   $susers_semails = $_POST['susers_semails'];
   $susers_spasswords = $_POST['susers_spasswords'];

   

   $stmt = $PDO->prepare("SELECT susers_sids,susers_semails,susers_spasswords FROM userstbl WHERE 
   susers_semails = :USER_EMAIL" );

   $stmt->bindParam(':USER_EMAIL',$susers_semails);
   $stmt->execute();
   $row = $stmt->fetch(PDO::FETCH_ASSOC); 
   

   if (!empty($row)) {

   if (password_verify($susers_spasswords, $row['susers_spasswords'])) {

     $returnApp = array( 'LOGIN' => 'Log_In_Success',$row);
     echo json_encode($returnApp);

    } 
    else {
    
     $returnApp = array( 'LOGIN' => 'Log_In_Failed');
     echo json_encode($returnApp);
    }

   } else {

     $returnApp = array( 'LOGIN' => 'Email_Doesnt_Exist');
     echo json_encode($returnApp);

  }

JSON

  {
  0: {
  susers_sids: "3"
  },
  LOGIN: "Log_In_Success" 
  }

Advertisement

Answer

$row become an attribue of the object $returnApp as an associative array. I think you try to pass only the suser_sids so it should be something like

$returnApp=array( 
    'LOGIN' => 'Log_In_Success', 
    'susers_sids' => $row['susers_sids']
); 

so it will return as :

{ 
    LOGIN: "Log_In_Success" , 
    susers_sids: "3"
}

Alternatively you could use array_merge_recursive to include the row within our object (as one associative array) but i won’t store/pass user password unless really necessary.

EDIT: Another way you can/should do it, is to create an actual object instead of using an assocaitive array. Something like this will create an anonymous object that you can pass to json_encode the same exact way you are doing now (welcome to OOP):

$returnAPP->LOGIN="Log_In_Success";
$returnAPP->susers_sids=$row['susers_sids'];
echo json_encode($returnApp);

You could even define such class but PHP being what it is, it will vreate the stdObject for you 😉

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement