我的应用程序中有一个AsyncTask类,但我注意到无论结果传递给onPostExecute函数是什么,它仍会指示应用程序转到else {},即使它完全符合if语句。为什么会这样?
这是我正在运行的AsyncTask:
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();
}
}
}
答案 0 :(得分:5)
您需要使用equals
方法来比较两个字符串:
if (result.equals("YES"))
和
if (result.equals("NO"))