0

I cast an object into a JSONObject then get the string and compare it with "play_audio." The IF statement gives a false result even though my log is giving me correct string - what is going on?

                JSONObject data = (JSONObject)args[0];
                String command = null;
                try {
                    command = data.getString("command");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "<<<<---- RECEIVING COMMAND ----->>> " + command);
                if (command == "ping_audio") { //WHY IS THIS FALSE?
                    Log.i(TAG, "<<<<---- PLAYING AUDIO ----->>> ");
                }

logcat

<<<<---- RECEIVING COMMAND ----->>> ping_audio
physiii
  • 209
  • 3
  • 12
  • This might help: http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java – justAbit Apr 22 '16 at 18:51

1 Answers1

1

try using the String.equals() method instead like command.equals("ping_audio")

if (command.equals("ping_audio") {    // should give you what you want
     Log.i(TAG, "<<<<---- PLAYING AUDIO ----->>> ");
}
kimchibooty
  • 339
  • 1
  • 11