2014-01-15 248 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");