Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.shotlu.heartrate;
- 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;
- MainActivity mainActivity;
- Reistration reistration;
- boolean check = false;
- public LoginResponse loginResponse = null;
- String userPhone;
- BackGroundTask(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 regUrl = "http://192.168.1.101:8080/MyHeartRate/registration.php";//192.168.1.102
- String loginUrl = "http://192.168.1.101:8080/MyHeartRate/login.php";//192.168.1.102
- String method = params[0];
- //////////////////////////For Registration//////////////////////////
- if (method.equals("registration")) {
- String userName = params[1];
- String userEmail = params[2];
- userPhone = params[3];
- //int userAge = Integer.parseInt(params[4]);
- String userAge = params[4];
- String userPass = params[5];
- try {
- URL url = new URL(regUrl);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
- String data = URLEncoder.encode("Name", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8")
- + "&" + URLEncoder.encode("Email", "UTF-8") + "=" + URLEncoder.encode(userEmail, "UTF-8")
- + "&" + URLEncoder.encode("Phone", "UTF-8") + "=" + URLEncoder.encode(userPhone, "UTF-8")
- + "&" + URLEncoder.encode("Age", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(userAge), "UTF-8")
- + "&" + URLEncoder.encode("Pass", "UTF-8") + "=" + URLEncoder.encode(userPass, "UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
- String response = "";
- String line = "";
- while ((line = bufferedReader.readLine()) != null) {
- response += line;
- }
- bufferedReader.close();
- IS.close();
- httpURLConnection.disconnect();
- return response;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //////////////////////////For Login///////////////////////////
- else if (method.equals("login")) {
- 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("Registration Successfull!!"))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- alertDialog.setMessage(result);
- alertDialog.show();
- }
- else if (result.equals("Registration failed!!!!") || result.equals("This Phone " + userPhone + " Number Already Exist..."))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- alertDialog.setMessage(result);
- alertDialog.show();
- }
- else if (result.equals("Login Success...Welcome"))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- check = true;
- }
- else if (result.equals("Login Failed......Try Again.."))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- check = false;
- }
- super.onPostExecute(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement