Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class PatLogin extends Activity {
- EditText a,b;
- String login_name,login_pass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.pat_login);
- }
- public void patbuttonClick(View v) {
- if (v.getId() == R.id.patlogin) {
- a = (EditText) findViewById(R.id.TFpusername);
- b = (EditText) findViewById(R.id.TFppassword);
- login_name = a.getText().toString();
- login_pass = b.getText().toString();
- String method = "login";
- BackgroundTask backgroundTask = new BackgroundTask(this);
- backgroundTask.execute(method,login_name,login_pass);
- //If possible I would like to call the "Display" activity from here but only when the correct username and password is entered.
- //If it's not possible to call from here then I would like to know how to call "Display" activity from "BackgrounTask.java".
- }
- }
- }
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- public class BackgroundTask extends AsyncTask<String,Void,String> {
- AlertDialog alertDialog;
- Context ctx;
- BackgroundTask(Context ctx)
- {
- this.ctx =ctx;
- }
- int flag=0;
- @Override
- protected void onPreExecute() {
- alertDialog = new AlertDialog.Builder(ctx).create();
- alertDialog.setTitle("Login Information....");
- }
- @Override
- protected String doInBackground(String... params) {
- String login_url = "http://10.0.2.2/mobidoc/login.php";
- String method = params[0];
- if(method.equals("login"))
- {
- String login_name = params[1];
- String login_pass = params[2];
- try {
- 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 data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
- URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
- String response = "";
- String line = "";
- while ((line = bufferedReader.readLine())!=null)
- {
- response+= line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return response;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String result) {
- if(result.equals("Registration Success..."))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- }
- else
- {
- alertDialog.setMessage(result);
- alertDialog.show();
- //When I use the following 2 lines of code, It calls the Display activity even if the wrong password is entered. I need a certain condition to be applied.
- Intent myIntent = new Intent(ctx, Display.class);
- ctx.startActivity(myIntent);
- }
- }
- }
- <?php
- require "init.php";
- $username = $_POST["login_name"];
- $password = $_POST["login_pass"];
- $password = md5($password);
- $sql_query = "select name from pat_info where username like '$username' and password like '$password';";
- $result = mysqli_query($con,$sql_query);
- if(mysqli_num_rows($result)>0)
- {
- $row = mysqli_fetch_assoc($result);
- $name = $row["name"];
- echo "Login Success... Welcome ".$name;}
- else{
- echo "Login Failed...Try Again.";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement