0

I have manage to make work a login screen taking the java script from here enter link description here but my problem is that even if the login is wrong the new activity I am requesting opens.

Here is the code,

package com.xxxxxx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Admin extends Activity {
    /** Called when the activity is first created. */
    private Button login;
    private EditText username, password;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin);

        login = (Button) findViewById(R.id.login);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String   mUsername = username.getText().toString();
                String  mPassword = password.getText().toString();

                tryLogin(mUsername, mPassword);
            }
        });
    }

    protected void tryLogin(String mUsername, String mPassword)
    {           
        HttpURLConnection connection;
       OutputStreamWriter request = null;

            URL url = null;   
            String response = null;         
            String parameters = "username="+mUsername+"&password="+mPassword;   

            try
            {
                url = new URL("http://xxxxxxxxxxxxxxx/login.php");
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestMethod("POST");    

                request = new OutputStreamWriter(connection.getOutputStream());
                request.write(parameters);
                request.flush();
                request.close();            
                String line = "";               
                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                // Response from server after login process will be stored in response variable.                
                response = sb.toString();
                // You can perform UI operations here
                Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
                isr.close();
                reader.close();
            Intent intent = new Intent (Admin.this, Webview.class);
            startActivity(intent);
            finish();
            }
            catch(IOException e)
            {
                // Error
            }
    }
}

There is something wrong with the startactivity I guess but I cannot fix it. Thanks in advance.

And here is the php

<?php

$username = $_POST['username'];
$password = $_POST['password'];

if($username=='xxxxxxx' && $password == 'xxxxxxxx'){
echo "true";
}
else{
echo "Login Failed";
}

?>
Community
  • 1
  • 1
Christopher
  • 25
  • 1
  • 2
  • 9

3 Answers3

1

There doesn't seem to be anything wrong with the startActivity call. startActivity has no idea if your login was correct or not, it is set to always fire so that's what it does each and every time.

You probably want to check either the HTTP response code from your login attempt, or parse the output from the login attempt to see whether or not it was successful. Using either of those approaches, you can then choose to call startActivity if and only if the login attempt was successful.

Edit:
Based on your code, the String response should contain either "true" or "Login Failed". Check the contents of response and only call startActivity if response is "true", perhaps doing something else in the case of "Login Failed" to notify the user.

Marc Bernstein
  • 11,423
  • 5
  • 34
  • 32
1

The http request won't throw an error if the login is unsuccessful. You'll most likely get back a 401 code from the server. You'll need to handle that code from the server in your logic. Also, you may want to have your tryLogin method just return true or false and then have an if statement to start the Activity or not.

SBerg413
  • 14,515
  • 6
  • 62
  • 88
1

No buddy... it will always start the activity that you mentioned there except any exception occurs while trying to get the response from the server... Now what has been happenning you are getting some sort of response from the server even if the login fails. All you need is to have an condition to check whether the login is successful before starting the new activity...

for Ex: Give an response from the server as "1" for Successful login and 0 for failure login.

Dinash
  • 3,027
  • 4
  • 32
  • 45