0

How do i get login credentials from server in an Android App instead using 'admin' and 'admin' credentials, i want to use user credentials which i get from the server, // Here i want to use user credentials which i get from the server

package slv.com.loginapp;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private EditText username;
    private EditText password;
    private Button login;
    private TextView loginLockedTV;
    private TextView attemptsLeftTV;
    private TextView numberOfRemainingLoginAttemptsTV;
    int numberOfRemainingLoginAttempts = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupVariables();
    }

    public void authenticateLogin(View view) {
        try {
            if (username.getText().toString().equals("admin") && password.getText().toString().equals("admin")) { 
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
                Toast.makeText(getApplicationContext(), "Hello admin!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Seems like you 're not admin!", Toast.LENGTH_SHORT).show();
                numberOfRemainingLoginAttempts--;
                attemptsLeftTV.setVisibility(View.VISIBLE);
                numberOfRemainingLoginAttemptsTV.setVisibility(View.VISIBLE);
                numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
            }

            if (numberOfRemainingLoginAttempts == 0) {
                login.setEnabled(false);
                loginLockedTV.setVisibility(View.VISIBLE);
                loginLockedTV.setBackgroundColor(Color.RED);
                loginLockedTV.setText("LOGIN LOCKED!!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setupVariables() {
        username = (EditText) findViewById(R.id.usernameET);
        password = (EditText) findViewById(R.id.passwordET);
        login = (Button) findViewById(R.id.loginBtn);
        loginLockedTV = (TextView) findViewById(R.id.loginLockedTV);
        attemptsLeftTV = (TextView) findViewById(R.id.attemptsLeftTV);
        numberOfRemainingLoginAttemptsTV = (TextView) findViewById(R.id.numberOfRemainingLoginAttemptsTV);
        numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
    }

}
Santosh Jadi
  • 1,401
  • 6
  • 28
  • 52
  • Are you trying to implement server-side authentication for your app? – Owais Ali Jan 12 '16 at 06:47
  • first, you need a server and database that contains user data – Randyka Yudhistira Jan 12 '16 at 06:49
  • @OwaisAli: yes.. from where i shud start?? – Santosh Jadi Jan 12 '16 at 06:55
  • @RandykaYudhistira ya..im new to it,, how can get it? – Santosh Jadi Jan 12 '16 at 06:57
  • Is there already a service implemented you want to connect to or will you be creating the service? If you're new at this, standing up the service may be a lot of work (depending on what you're trying to do) - you can see if integrating with some other provider (e.g. OAuth, Facebook) meets your requirements. – Owais Ali Jan 12 '16 at 16:58
  • If you want to proceed with setting up your own service, create the web service which takes these parameters and then consume that service in your Android app using one of the many mechanisms to consume web APIs (e.g. Retrofit). – Owais Ali Jan 12 '16 at 16:59
  • @OwaisAli hmmm how abt adding credentials to json in a server and later it can be fetched using URL and HttpUrlConnection... or any other better Suggestion?? – Santosh Jadi Jan 13 '16 at 04:50
  • It depends on your specific requirements, but if you're sending credentials down to the device (and then checking them on the client side) it is not going to be very secure. – Owais Ali Jan 13 '16 at 07:38
  • im doing only for practice purpose,, how best i can do according to android coding standards? – Santosh Jadi Jan 13 '16 at 07:44

1 Answers1

0

Got the Solution..;) Can get login credentials from server using AsyncTask. Go for below Link

MySQL Connection to an Android App using AsyncTask with Android Studio

Community
  • 1
  • 1
Santosh Jadi
  • 1,401
  • 6
  • 28
  • 52