我正在编写一个程序来验证无法正常运行的登录。没有出现任何错误,但它不会生成错误登录的Toast文本。我认为条件状态出错了。如果登录错误,它将在日志中显示“null”。
但我无法找到错误的位置,有人可以帮忙吗?
HttpClient client = new DefaultHttpClient();
try {
ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("login", loginText));
parameters.add(new BasicNameValuePair("password", pwText));
String url = controller.bniRootUrl+
controller.folderUser+
controller.fileLogin+
"?";
url += URLEncodedUtils.format(parameters, "utf-8");
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
loginResponse = EntityUtils.toString(response.getEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "response: "+loginResponse);
if (loginResponse == null || loginResponse == "null" || loginResponse.isEmpty()){
Toast.makeText(Login.this, "invalid login", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
可能,loginResponse是一个String变量。所以,这不是比较两个字符串是否相等的正确方法。因此,这种情况在运行时未得到验证。
我们使用public boolean equals(String secondString)
方法将firstString与secondString进行比较。
尝试将if语句更改为
if (loginResponse.equals(null) || loginResponse.equals("null") || loginResponse.isEmpty())
{...}