Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- Button loginBtn, registerBtn;
- EditText usernameET, passwordET;
- TextView forgotTV;
- String works = "";
- String username, password;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- usernameET = (EditText) findViewById(R.id.usernameET);
- passwordET = (EditText) findViewById(R.id.passwordET);
- loginBtn = (Button) findViewById(R.id.loginBtn);
- loginBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- username = usernameET.getText().toString();
- password = passwordET.getText().toString();
- String method = "login";
- BackgroundTask backgroundTask = new
- BackgroundTask(MainActivity.this);
- backgroundTask.execute(method, username, password);
- if(true) {
- Intent intent = new Intent(MainActivity.this, Home.class);
- startActivity(intent);
- }
- }
- });
- registerBtn = (Button) findViewById(R.id.registerBtn);
- registerBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(MainActivity.this, Register.class));
- }
- });
- forgotTV = (TextView) findViewById(R.id.forgotBtn);
- forgotTV.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(MainActivity.this,
- ForgotPassword.class));
- }
- });
- }
- public class BackgroundTask extends AsyncTask<String, Void, String> {
- Context context;
- AlertDialog alertDialog;
- BackgroundTask(Context ctx) {
- context = ctx;
- }
- @Override
- protected String doInBackground(String... params) {
- String registrationURL = "http://192.168.0.15:8080/imo/register.php";
- String loginURL = "http://192.168.0.15:8080/imo/login.php";
- String postURL = "http://192.168.0.15:8080/imo/postbox.php";
- String line = "";
- String result = "";
- String type = params[0];
- if (type.equals("login")) {
- String username = params[1];
- String password = params[2];
- try {
- URL url = new URL(loginURL);
- HttpURLConnection httpURLConnection = (HttpURLConnection)
- url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new
- OutputStreamWriter(OS, "UTF-8"));
- String postData = URLEncoder.encode("username", "UTF-8") + "=" +
- URLEncoder.encode(username, "UTF-8") + "&"
- + URLEncoder.encode("password", "UTF-8") + "=" +
- URLEncoder.encode(password, "UTF-8");
- writer.write(postData);
- writer.flush();
- writer.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- BufferedReader reader = new BufferedReader(new
- InputStreamReader(IS, "iso-8859-1"));
- while ((line = reader.readLine()) != null) {
- result += line;
- }
- reader.close();
- IS.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if (type.equals("register")) {
- String username = params[1];
- String password = params[2];
- String email = params[3];
- try {
- URL url = new URL(registrationURL);
- HttpURLConnection httpURLConnection = (HttpURLConnection)
- url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new
- OutputStreamWriter(OS, "UTF-8"));
- String postData = URLEncoder.encode("username", "UTF-8") + "=" +
- URLEncoder.encode(username, "UTF-8") + "&"
- + URLEncoder.encode("password", "UTF-8") + "=" +
- URLEncoder.encode(password, "UTF-8") + "&"
- + URLEncoder.encode("email", "UTF-8") + "=" +
- URLEncoder.encode(email, "UTF-8");
- writer.write(postData);
- writer.flush();
- writer.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- BufferedReader reader = new BufferedReader(new
- InputStreamReader(IS, "iso-8859-1"));
- while ((line = reader.readLine()) != null) {
- result += line;
- }
- reader.close();
- IS.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- else if(type.equals("postbox")){
- String body = params[1];
- try {
- URL url = new URL(postURL);
- HttpURLConnection httpURLConnection = (HttpURLConnection)
- url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new
- OutputStreamWriter(OS, "UTF-8"));
- String postData = URLEncoder.encode("body", "UTF-8") + "=" +
- URLEncoder.encode(body, "UTF-8");
- writer.write(postData);
- writer.flush();
- writer.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- BufferedReader reader = new BufferedReader(new
- InputStreamReader(IS, "iso-8859-1"));
- while ((line = reader.readLine()) != null) {
- result += line;
- }
- reader.close();
- IS.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("LOG");
- }
- @Override
- protected void onPostExecute (String result){
- alertDialog.setMessage(result);
- alertDialog.show();
- }
- @Override
- protected void onProgressUpdate (Void...values){
- super.onProgressUpdate(values);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement