我正在编写一个程序来验证无法正常登录。不会发生任何错误,但不会为不正确的登录生成吐司文本。我认为条件状态出问题了。如果登录错误,它将响应日志中显示的“ 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();
}
loginResponse可能是一个String变量。因此,这不是比较两个字符串是否相等的正确方法。因此,此条件在运行时未得到验证。。
我们使用public boolean equals(String secondString)
方法将firstString与secondString进行比较。尝试将if语句更改为
if (loginResponse.equals(null) || loginResponse.equals("null") || loginResponse.isEmpty())
{...}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句