我的Android应用上有一个忘记密码页面。如果我没有输入电子邮件,它会从服务器返回正确的响应,如果我输入在我们的数据库中找到的电子邮件,那么它会向用户发送电子邮件并从服务器返回正确的响应。但是,如果我输入电子邮件但在我们的数据库中找不到它,那么当我打电话
时HttpEntity entity = response.getEntity();
entity是一个空值。我不确定为什么它适用于其中两个案例但不适用于第三个案例。
有谁知道为什么会这样?我的代码如下
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;
非常感谢任何帮助解决这个问题,谢谢!
答案 0 :(得分:1)
据我所知,它的行为符合HttpEntity
的规范,适用于205条回复。这是规范所说的:
HTTP消息可以携带与请求关联的内容实体 或回应。在某些请求和某些请求中可以找到实体 答案,因为它们是可选的。使用实体的请求是 称为封闭请求的实体。 HTTP规范 定义了两个封闭请求方法的实体:POST和PUT。回应 通常期望包含内容实体。 有例外 这个规则,如对HEAD方法的响应和204 No Content,304 未修改, 205重置内容响应。
如果未找到电子邮件,您可以使用PHP发送404响应代码并检入Java代码:
if(response.getStatusLine().getStatusCode() == 404){
//email was not found
}
答案 1 :(得分:1)
为未找到的电子邮件发送相同的200代码
sendResponse(200, "Email Address Not Found");