-5

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

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

根据我从 php 脚本获得的响应的整数值,我执行后续任务。即使成功地将值插入数据库并且第一个打印语句将 Regresult 的值显示为“2”,if 语句也不起作用,并且 else 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);
 }
4

1 回答 1

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();
        }
于 2013-10-03T06:16:54.463 回答