Login with Android PHP and Mysql

3

Hello! Following a tutorial I created an app that includes registration and login with PHP and MYSQL android, the registration works perfectly, except that in the login part when the inserted data is correct instead of taking me to another screen only notifies me if the login was done either successfully or not.

How do I fix this? Here is my code.

MainAcitivity

public class MainActivity extends ActionBarActivity {

    EditText UsernameEt, PasswordEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsernameEt = (EditText)findViewById(R.id.etUserName);
        PasswordEt = (EditText)findViewById(R.id.etPassword);

    }


    public void OnLogin(View view) {
        String username = UsernameEt.getText().toString();
        String password = PasswordEt.getText().toString();
        String type = "login";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);



    }

}

BackgroundWorker

public class BackgroundWorker extends AsyncTask<String,Void,String> {

    Context context;

    AlertDialog alertDialog;

    BackgroundWorker (Context ctx) {
        context = ctx;

    }

    @Override
    protected String doInBackground(String... params) {
        String type = params[0];

        String login_url = "http://10.127.127.1/ws/login.php";

        if(type.equals("login")) {
            try {

                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";

                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;


            } catch (MalformedURLException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();

            }
        }


        return null;
    }


    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Status do login");

    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }


}

And here's my php.

<?php 
require "conn.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from login where username like '$user_name' and password like '$user_pass';";
$result = mysqli_query($conn ,$mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo "login success !!!!! Welcome user";
}
else {
echo "login not success";
}

?>
    
asked by anonymous 14.08.2017 / 17:27

1 answer

5

Based on the response that is returning from your PHP, within the onPostExecute() method you simply create a condition. For example:

if(result.equals("login success !!!!! Welcome user")){
    // aqui o redirecionamento para a activity desejada
    Intent i = new Intent(context, ActivityDepoisDoLogin.class);
    context.startActivity(i);
} else {
    // aqui pode colocar a mensagem dizendo que as credenciais estão incorretas
}
    
14.08.2017 / 17:33