Android:如果条件正确,则跳过语句

时间:2013-10-03 06:03:40

标签: java php android mysql if-statement

我是Android的新手,正在创建一个注册页面。由于android不支持mysql,我创建了一个基于php的脚本,它为不同的查询提供了整数值。

2 --> successful insertion in database
-1 --> Email already used
0--> username used

根据我从php脚本获得的响应的整数值,我执行后续任务。即使成功地在数据库中插入值并且第一个打印版本将Regresult的值显示为“2”,if语句也不起作用,并且其他if语句都正常工作。

/* Getting the response back from the PHPAPI and storing it in a variable */
HttpEntity entity = Response.getEntity();
String Regresult = EntityUtils.toString(entity);


/* printing value of Regresult to verify if the condition is right or wrong */
System.out.println(Regresult);

/* Registration depending upon response from Http post call */       
/* Email used works fine when Regresult = -1 */

if(Regresult.equals("-1"))
{
     Context text = getApplicationContext();
     CharSequence message = "Email already in use.";
     int duration = Toast.LENGTH_SHORT;
     Toast toast = Toast.makeText(text, message, duration);
     toast.show();
 }

 /* Username used work fine when Regresult = 0 */

 else if(Regresult.equals("0"))
 {
      Context text = getApplicationContext();
      CharSequence message = "Username already in use.";
      int duration = Toast.LENGTH_SHORT;
      Toast toast = Toast.makeText(text, message, duration);
      toast.show();
 }  

/* Here is the problem when Regresult= 2 , even though consition is correct the statement don't get executed */
 else if(Regresult.equals("2"))
 {
      Context text = getApplicationContext();
      CharSequence message = "Registration Successful.";
      int duration = Toast.LENGTH_SHORT;
      Toast toast = Toast.makeText(text, message, duration);
      toast.show();
      System.out.println(Regresult);
 }

1 个答案:

答案 0 :(得分:0)

// I think it may be some spacing problem so try this way
        if(Regresult.trim().equals("-1")){
            Context text = getApplicationContext();
            CharSequence message = "Email already in use.";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(text, message, duration);
            toast.show();
        }else if(Regresult.trim().equals("0")){
            Context text = getApplicationContext();
            CharSequence message = "Username already in use.";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(text, message, duration);
            toast.show();
        }else if(Regresult.trim().equals("2")) {
            Context text = getApplicationContext();
            CharSequence message = "Registration Successful.";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(text, message, duration); toast.show();
        }