Advertisement
asadujjaman

Untitled

May 3rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.61 KB | None | 0 0
  1. package com.example.shotlu.heartrate;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.widget.Toast;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.BufferedWriter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.OutputStream;
  15. import java.io.OutputStreamWriter;
  16. import java.net.HttpURLConnection;
  17. import java.net.MalformedURLException;
  18. import java.net.URL;
  19. import java.net.URLEncoder;
  20.  
  21. public class BackGroundTask extends AsyncTask<String,Void,String> {
  22.     AlertDialog alertDialog;
  23.     Context ctx;
  24.     MainActivity mainActivity;
  25.     Reistration reistration;
  26.     boolean check = false;
  27.     public LoginResponse loginResponse = null;
  28.  
  29.     String userPhone;
  30.  
  31.     BackGroundTask(Context ctx) {
  32.         this.ctx = ctx;
  33.     }
  34.  
  35.  
  36.     @Override
  37.     protected void onPreExecute() {
  38.         alertDialog = new AlertDialog.Builder(ctx).create();
  39.         alertDialog.setTitle("Login Information...");
  40.     }
  41.  
  42.     @Override
  43.     protected String doInBackground(String... params) {
  44.  
  45.         String regUrl = "http://192.168.1.101:8080/MyHeartRate/registration.php";//192.168.1.102
  46.         String loginUrl = "http://192.168.1.101:8080/MyHeartRate/login.php";//192.168.1.102
  47.  
  48.         String method = params[0];
  49.  
  50.  
  51.         //////////////////////////For Registration//////////////////////////
  52.         if (method.equals("registration")) {
  53.             String userName = params[1];
  54.             String userEmail = params[2];
  55.             userPhone = params[3];
  56.             //int userAge = Integer.parseInt(params[4]);
  57.             String userAge = params[4];
  58.             String userPass = params[5];
  59.  
  60.             try {
  61.                 URL url = new URL(regUrl);
  62.                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  63.                 httpURLConnection.setRequestMethod("POST");
  64.                 httpURLConnection.setDoOutput(true);
  65.                 OutputStream OS = httpURLConnection.getOutputStream();
  66.                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  67.                 String data = URLEncoder.encode("Name", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8")
  68.                         + "&" + URLEncoder.encode("Email", "UTF-8") + "=" + URLEncoder.encode(userEmail, "UTF-8")
  69.                         + "&" + URLEncoder.encode("Phone", "UTF-8") + "=" + URLEncoder.encode(userPhone, "UTF-8")
  70.                         + "&" + URLEncoder.encode("Age", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(userAge), "UTF-8")
  71.                         + "&" + URLEncoder.encode("Pass", "UTF-8") + "=" + URLEncoder.encode(userPass, "UTF-8");
  72.                 bufferedWriter.write(data);
  73.                 bufferedWriter.flush();
  74.                 bufferedWriter.close();
  75.                 OS.close();
  76.                 InputStream IS = httpURLConnection.getInputStream();
  77.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
  78.                 String response = "";
  79.                 String line = "";
  80.                 while ((line = bufferedReader.readLine()) != null) {
  81.                     response += line;
  82.                 }
  83.                 bufferedReader.close();
  84.                 IS.close();
  85.                 httpURLConnection.disconnect();
  86.                 return response;
  87.             } catch (MalformedURLException e) {
  88.                 e.printStackTrace();
  89.             } catch (IOException e) {
  90.                 e.printStackTrace();
  91.             }
  92.  
  93.         }
  94.  
  95.  
  96.         //////////////////////////For Login///////////////////////////
  97.  
  98.         else if (method.equals("login")) {
  99.             String loginUsername = params[1];
  100.             String loginPassword = params[2];
  101.  
  102.             try {
  103.                 URL url = new URL(loginUrl);
  104.                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  105.                 httpURLConnection.setRequestMethod("POST");
  106.                 httpURLConnection.setDoOutput(true);
  107.                 httpURLConnection.setDoInput(true);
  108.                 OutputStream outputStream = httpURLConnection.getOutputStream();
  109.                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  110.  
  111.                 String data = URLEncoder.encode("Phone", "UTF-8") + "=" + URLEncoder.encode(loginUsername, "UTF-8")
  112.                         + "&" + URLEncoder.encode("Pass", "UTF-8") + "=" + URLEncoder.encode(loginPassword, "UTF-8");
  113.  
  114.                 bufferedWriter.write(data);
  115.                 bufferedWriter.flush();
  116.                 bufferedWriter.close();
  117.                 outputStream.close();
  118.                 InputStream inputStream = httpURLConnection.getInputStream();
  119.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  120.                 String response = "";
  121.                 String line = "";
  122.                 while ((line = bufferedReader.readLine()) != null) {
  123.                     response += line;
  124.                 }
  125.                 bufferedReader.close();
  126.                 inputStream.close();
  127.                 httpURLConnection.disconnect();
  128.                 return response;
  129.             } catch (MalformedURLException e) {
  130.                 e.printStackTrace();
  131.             } catch (IOException e) {
  132.                 e.printStackTrace();
  133.             }
  134.         }
  135.         return null;
  136.     }
  137.  
  138.     @Override
  139.     protected void onProgressUpdate(Void... values) {
  140.         super.onProgressUpdate(values);
  141.     }
  142.  
  143.     @Override
  144.     protected void onPostExecute(String result)
  145.     {
  146.  
  147.         if (result.equals("Registration Successfull!!"))
  148.         {
  149.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  150.             alertDialog.setMessage(result);
  151.             alertDialog.show();
  152.         }
  153.         else if (result.equals("Registration failed!!!!") || result.equals("This Phone " + userPhone + " Number Already Exist..."))
  154.         {
  155.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  156.             alertDialog.setMessage(result);
  157.             alertDialog.show();
  158.         }
  159.         else if (result.equals("Login Success...Welcome"))
  160.         {
  161.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  162.             check = true;
  163.         }
  164.         else if (result.equals("Login Failed......Try Again.."))
  165.         {
  166.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  167.             check = false;
  168.         }
  169.         super.onPostExecute(result);
  170.     }
  171.  
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement