You are on page 1of 3

package jairoe.example.com.

yu_ay;
import
import
import
import
import

android.content.Context;
android.content.Intent;
android.os.AsyncTask;
android.support.v7.app.AlertDialog;
android.view.View;

import
import
import
import
import
import
import
import
import
import
import

java.io.BufferedReader;
java.io.BufferedWriter;
java.io.IOException;
java.io.InputStream;
java.io.InputStreamReader;
java.io.OutputStream;
java.io.OutputStreamWriter;
java.net.HttpURLConnection;
java.net.MalformedURLException;
java.net.URL;
java.net.URLEncoder;

/**
* Created by JKAS_ on 21/12/2016.
*/
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 email = params[1];
String password =
params[2];
String login_url = "http://192.168.1.4/login.php";
String register_url = "http://192.168.1.4/register.php";
if(type.equals("register")) {
try {
String firstName = params[3];
String lastName = params[4];
String contactNumber = params[5];
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.op
enConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStr
eamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("email", "UTF-8") + "=" + U
RLEncoder.encode(email, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEnco
der.encode(password, "UTF-8") + "&"
+ URLEncoder.encode("firstname", "UTF-8") + "=" + URLEnc
oder.encode(firstName, "UTF-8") + "&"
+ URLEncoder.encode("lastname", "UTF-8") + "=" + URLEnco
der.encode(lastName, "UTF-8") + "&"

+ URLEncoder.encode("contactnumber", "UTF-8") + "=" + UR


LEncoder.encode(contactNumber, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStre
amReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(type.equals("login")) {
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.op
enConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStr
eamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("email", "UTF-8") + "=" + U
RLEncoder.encode(email, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEnco
der.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStre
amReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login status");
}
@Override
protected void onPostExecute(String result) {
if(result.equals("login success")) {
Intent in = new Intent(context, Register.class);
context.startActivity(in);
}
else if (result.equals("login not success")){
alertDialog.setMessage("Wrong email/password. Please try again");
alertDialog.show();
}
else if (result.equals("insert success")){
Intent in = new Intent(context, Register.class);
context.startActivity(in);
}
else if (result.equals("insert not success")){
alertDialog.setMessage("Registration not complete. . . Please try ag
ain");
alertDialog.show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}

You might also like