Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.shotlu.heartrate;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- 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 MainActivity extends Activity{
- EditText etPhoneNumber,etPassword;
- String loginUserName, loginPassword;
- boolean isLoggedIn = false;
- LogIn logIn = new LogIn();
- Button btLogin;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etPhoneNumber = (EditText) findViewById(R.id.etPhone);
- etPassword = (EditText) findViewById(R.id.etPass);
- }
- public void userReg(View view)
- {
- startActivity(new Intent(this,Reistration.class));
- }
- public void userLogin(View view)
- {
- loginUserName = "01823105205";//etPhoneNumber.getText().toString();//
- loginPassword = "dm123456";//etPassword.getText().toString();//
- String method="login";
- Bundle basket = new Bundle();
- basket.putString("loginUserName",loginUserName);
- FormValidation formValidation = new FormValidation(loginUserName,loginPassword);
- if(formValidation.ValidateLogin())
- {
- // String method = "login";
- // BackGroundTask backGroundTask = new BackGroundTask(this);
- // backGroundTask.execute(method, loginUserName, loginPassword);
- new LoginBackGroundTask(this).execute(method,loginUserName,loginPassword);
- }
- else
- {
- Toast.makeText(getBaseContext(), formValidation.ValidateLoginMessage(), Toast.LENGTH_LONG).show();
- }
- }
- class LoginBackGroundTask extends AsyncTask<String,Void,String>
- {
- AlertDialog alertDialog;
- Context ctx;
- Reistration reistration;
- String userPhone;
- LoginBackGroundTask(Context ctx)
- {
- this.ctx = ctx;
- }
- @Override
- protected void onPreExecute()
- {
- alertDialog = new AlertDialog.Builder(ctx).create();
- alertDialog.setTitle("Login Information...");
- }
- @Override
- protected String doInBackground(String... params)
- {
- String loginUrl = "http://192.168.1.101:8080/MyHeartRate/login.php";//192.168.1.102
- String method = params[0];
- String loginUsername = params[1];
- String loginPassword = params[2];
- try
- {
- URL url = new URL(loginUrl);
- 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("Phone", "UTF-8") + "=" + URLEncoder.encode(loginUsername, "UTF-8")
- + "&" + URLEncoder.encode("Pass", "UTF-8") + "=" + URLEncoder.encode(loginPassword, "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("Login Success...Welcome"))
- {
- //Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- IsLoggedIn();
- }
- else if (result.equals("Login Failed......Try Again.."))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- }
- }
- }
- public void IsLoggedIn()
- {
- finish();
- Toast.makeText(this.getApplicationContext(), "LOG IN SUCCESSFULL", Toast.LENGTH_LONG).show();
- Intent i = new Intent(getApplicationContext(), PatientPulseDetails.class);
- i.putExtra("new_variable_name", loginUserName);
- startActivity(i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement