I create new project in Android Studio. Did login Page. Did php file to check if useame and password is Correct.
<?php
require "co.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from employee_data where useame like '$user_name'and password like '$user_pass'";
$result = mysqli_query($co ,$mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo"login success";
}else {
echo "login not success";
}
?>
My java Code in Android Studio
public class MainActivity extends AppCompatActivity {
EditText etnomedeutilizador, etpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etnomedeutilizador = (EditText)findViewById(R.id.etnomedeutilizador);
etpassword = (EditText)findViewById(R.id.etpassword);
}
public void botaologinonClick(View view) {
String useame = etnomedeutilizador.getText().toString();
String password = etpassword.getText().toString();
String type = "login";
BackGroundWorker backGroundWorker = new BackGroundWorker(this);
backGroundWorker.execute(type, useame, password);
System.err.println("aspas");
My java (BackGroundWorker) code is this
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://192.168.1.79/login.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLCoection httpURLCoection = (HttpURLCoection) url.openCoection();
httpURLCoection.setRequestMethod("POST");
httpURLCoection.setDoOutput(true);
httpURLCoection.setDoInput(true);
OutputStream outputStream = httpURLCoection.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");
String user = URLEncoder.encode(user_name, "UTF-8");//guarda o nome de utilizador introduzido
String pass = URLEncoder.encode(password, "UTF-8");//guarda a pass introduzido
System.err.println("------------------------------------------");
/*/ if(user.equals("Nome de Utilizador")){
if(pass.equals("Password")) {
System.err.println("Entrou no if");
startActivity(new Intent(this, Pagina1.class));}
/*/
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLCoection.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();
httpURLCoection.discoect();
retu result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Question: If my useame and password are correct with my DataBase(localhost) i want to start new activitie in my Android Studio after message shown from php (Login Success). Login Success if useame and/or password wrong show message from php (login not success). Wrong Password
To me more specified, application stays in "Login Success" message from php and i want it to start new activitie java in my Android after that.
