2014-01-15 250 views
0

我在我的Android应用程序中忘记了密码页面。如果我没有输入任何电子邮件,它会从服务器返回正确的响应,如果我输入了在我们的数据库中找到的电子邮件,那么它会向用户发送一封电子邮件并从服务器返回正确的响应。但是,如果我在一封电子邮件中输入,它是不是在我们的数据库中发现,当我打电话HTTPEntity给出空值

HttpEntity entity = response.getEntity(); 

实体为空值。我不确定为什么它可以用于2个案例,但不是第三个。

有谁知道这是为什么?我的代码如下

的Android代码:

private void accessURL(String url) { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 

    if (url.equalsIgnoreCase(Global.forgotPasswordURL)) { 
     InputStream is = null; 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString())); 

     try { 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 

      if(entity != null){ 
       is = entity.getContent(); 
       String jsonResult = inputStreamToString(is).toString(); 
       if (jsonResult.equalsIgnoreCase("Please enter your Email")) { 
        new AlertDialog.Builder(this).setTitle("Please Enter Your Email Address") 
          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int which) { 
            // continue with delete 
           } 
          }).show(); 
       }else if(jsonResult.equalsIgnoreCase("Email Address Not Found")){ 
        new AlertDialog.Builder(this).setTitle("The Email Address You Entered has not Been Found").setMessage("Make sure that you entered your email correctly.") 
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

         } 
        }).show(); 
       }else{ 
        new AlertDialog.Builder(this).setTitle("Your Email Has Been Found!").setMessage("Check the email you provied for further instructions on resetting your password.") 
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

         } 
        }).show(); 
       } 
      }else{ 
       Log.d("Null", "null"); 
      } 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

PHP代码:

if (isset($_POST["email"]) && !empty($_POST['email'])) { 
     $con=mysqli_connect("localhost","***********","******","*******"); 
     $email = $_POST['email']; 
     $query = mysql_query("SELECT * FROM users WHERE email = '$email'"); 
     $query2 = mysql_query("SELECT * FROM tempusers WHERE email = '$email'"); 
     $ans = mysql_num_rows($query); 
     $ans2 = mysql_num_rows($query2); 
     $str = $ans . " " . $ans2; 
     if(mysql_num_rows($query) == 0 && mysql_num_rows($query2) == 0){ 
      sendResponse(205, "Email Address Not Found"); 
      return false; 
     } 
     $temp = false; 
     if(mysql_num_rows($query2) != 0){ 
      $temp = true; 
      $query1 = mysql_query("SELECT * FROM tempusers WHERE email = '$email'"); 
      $row = mysql_fetch_array($query1); 
      mailUser($email, $row['firstname'], $temp); 

      sendResponse(200, "Email Address Found".$str); 
      return true; 
     }else{ 
      $query1 = mysql_query("SELECT * FROM users WHERE email = '$email'"); 
      $row = mysql_fetch_array($query1); 
      mailUser($email, $row['firstname'], $temp); 

      sendResponse(200, "Email Address Found".$str); 
      return true; 
     } 
    } 
    sendResponse(400, 'Please enter your Email'); 
    return false; 

任何解决这个帮助将不胜感激,谢谢!

回答

1

据我所知,它的行为符合HttpEntity规定的205条回应。以下是规范说明:

HTTP消息可以携带与请求 或响应相关联的内容实体。实体可以在一些请求和一些 响应中找到,因为它们是可选的。使用实体的请求是 ,称为实体封闭请求。 HTTP规范 定义了两个实体封闭请求方法:POST和PUT。通常预期响应 将包含内容实体。 也有例外 对此规则,如响应HEAD方法和204无内容,304 未修改,205重置内容响应。

在情况下,如果电子邮件没有被发现可以用PHP发送404响应代码和Java代码检查:

if(response.getStatusLine().getStatusCode() == 404){ 
    //email was not found 
} 
+0

谢谢,这工作!我使状态码与其他状态码不同的唯一原因是因为在iPhone版本的应用程序中,我需要第三个状态码来区分这三种不同的情况,所以我只是选择了另一个代码。 – shadowarcher

1

发送相同的200码的电子邮件没有发现以及

sendResponse(200, "Email Address Not Found");