-1

I am writing a program to validate log-in which cannot work. Not occurring any error but it does not generate toast text for incorrect log-in. I consider it goes wrong on condition state. If the log-in is wrong, it will response a 'null' as it display in the log.

But I can't find where the fault is, can somebody help?

        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();
        }
Simon
  • 481
  • 2
  • 9
  • 20
  • 1
    First, you need to state what do you mean by "cannot work". What is the current behaviour and what is the expected? Also, what you need is probably try debugging it yourself. I see you've used some log to debug, if you add enough of them it'll eventually be very easy for you to spot what goes wrong. For example, log the input username/password, the url, the reposnse(which you did) and maybe a log line right after your toast line to make sure it enters the if{} – Jacky Cheng Jul 25 '14 at 02:56
  • edited the problem. the statement inside if-clause cannot be run which means the condition goes wrong but I don't know why it goes wrong... I have also tried putting log inside if-clause, it proves the condition really not work – Simon Jul 25 '14 at 03:03
  • What does the log statement `response: ` print? also, the second condition in the `if` statement should be modified. `String` checks for equality should be made with the `equals()` method. – asgs Jul 25 '14 at 03:06
  • @Simon-Check my answer! – Am_I_Helpful Jul 25 '14 at 03:11

1 Answers1

1

Probably,loginResponse is a String variable.So,that's not the correct way to compare two strings for equality. Hence,this condition is not getting verified at run-time. .

We use public boolean equals(String secondString) method to compare firstString with secondString. Try changing the if-statement to

if (loginResponse.equals(null) || loginResponse.equals("null") || loginResponse.isEmpty())
{...}
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73