0

I have an AsyncTask class in my application but what I noticed is that whatever the result passed on to to the onPostExecute function is, it will still direct the application to go to the else{} even though it perfectly fits the if statement. Why is that happening?

Here is the AsyncTask I am running :

class CheckPassword extends AsyncTask<String, String, String>{

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(ChooseTable.this);
    pDialog.setMessage("Checking Password. Please wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();
}

@Override
protected String doInBackground(String... p) {
    String password = p[0];
    int success;
    String access = "";

    try {
        // Building Parameter
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("password", password));

        //ipaddress of server

        Intent i = getIntent();
        Bundle b = i.getExtras();
        ipaddress = b.getString("IP_Address");

        if(ipaddress != "" || ipaddress != "...:")
        {
            // single table url
            String url = "http://"+ipaddress+"/MenuBook/checkSpecial.php";
            Log.d("URL", url + "");

            JSONObject json = jsonParser.makeHttpRequest(
                    url, "GET", params);
            Log.d("JSON OBJECT", json+"");
            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // successfully received table details
                JSONArray result = json
                        .getJSONArray("result"); // JSON Array

                // get table availability from JSON Array
                JSONObject accessObj = result.getJSONObject(0);
                access= accessObj.getString("allowAccess");

            }
            else
            {
                status = "TABLE NOT FOUND";
            }
        }
        else
        {
            status = "FAILED TO RETRIEVE SERVER IP ADDRESS";
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.d("ALLOW ACCESS", access+"");
    return access;
}

@Override
protected void onPostExecute(String result) {
    pDialog.dismiss();
    Log.d("RESULT", result+"");
    if(result == "YES"){
        Toast.makeText(ChooseTable.this, "ACCESS ALLOWED", Toast.LENGTH_LONG).show();
    }else if (result== "NO"){
        Toast.makeText(ChooseTable.this, "Access Denied", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(ChooseTable.this, status, Toast.LENGTH_LONG).show();
    }

}
}
Brent Worden
  • 10,624
  • 7
  • 52
  • 57
John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71

1 Answers1

5

You need to use the equals method to compare two Strings:

if (result.equals("YES"))

and

if (result.equals("NO"))
Phil
  • 35,852
  • 23
  • 123
  • 164