Java 提供空值的HTTPEntity
我的Android应用程序上有一个忘记密码页面。如果我没有输入电子邮件,它将从服务器返回正确的响应;如果我输入在数据库中找到的电子邮件,它将向用户发送电子邮件,并从服务器返回正确的响应。但是,如果我输入一封电子邮件,但在我们的数据库中找不到,当我打电话时Java 提供空值的HTTPEntity,java,php,android,Java,Php,Android,我的Android应用程序上有一个忘记密码页面。如果我没有输入电子邮件,它将从服务器返回正确的响应;如果我输入在数据库中找到的电子邮件,它将向用户发送电子邮件,并从服务器返回正确的响应。但是,如果我输入一封电子邮件,但在我们的数据库中找不到,当我打电话时 HttpEntity entity = response.getEntity(); 实体是空值。我不知道为什么它对其中两种情况有效,但对第三种情况无效 有人知道为什么会这样吗?我的代码如下 Android代码: private void ac
HttpEntity entity = response.getEntity();
实体是空值。我不知道为什么它对其中两种情况有效,但对第三种情况无效
有人知道为什么会这样吗?我的代码如下
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();
}
}
}
如果您能帮助解决此问题,我们将不胜感激,谢谢 据我所知,它的行为符合for 205响应的规范。以下是规格说明: HTTP消息可以携带与请求关联的内容实体 或回应。实体可以在某些请求和某些应用程序中找到 响应,因为它们是可选的。使用实体的请求是 称为包含请求的实体。HTTP规范 定义两个包含请求方法的实体:POST和PUT。响应 通常需要包含一个内容实体存在例外情况 对该规则的响应,例如对HEAD方法和204无内容的响应,304 未修改,205重置内容响应 如果未找到电子邮件,您可以使用PHP发送404响应代码,并检入Java代码:
if(response.getStatusLine().getStatusCode() == 404){
//email was not found
}
为未找到的电子邮件发送相同的200代码
sendResponse(200, "Email Address Not Found");
谢谢,成功了!我之所以将状态代码与其他代码区别开来,唯一的原因是在iPhone版本的应用程序中,我需要第三个状态代码来区分这三个不同的情况,所以我选择了另一个代码。
sendResponse(200, "Email Address Not Found");