Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nsu.visitorsys;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- 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;
- public class loginactivity extends Activity
- {
- EditText tusername, tpassword;
- Button btnlogin;
- SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
- SharedPreferences.Editor editor = pref.edit();
- public static final int CONNECTION_TIMEOUT = 10000;
- public static final int READ_TIMEOUT = 150000;
- public void onCreate(Bundle b)
- {
- super.onCreate(b);
- setContentView(R.layout.loginlayout);
- tusername = findViewById(R.id.tusername);
- tpassword = findViewById(R.id.tpassword);
- btnlogin = findViewById(R.id.btnlogin);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String username = tusername.getText().toString();
- String password = tpassword.getText().toString();
- if (username.length() == 0 || password.length() == 0)
- {
- Toast.makeText(getApplicationContext(),"username of password is missing", Toast.LENGTH_LONG).show();
- }
- else
- {
- new AsyncLogin().execute(tusername.getText().toString(), tpassword.getText().toString());
- }
- }
- });
- }
- private class AsyncLogin extends AsyncTask<String, String, String>
- {
- ProgressDialog pdLoading = new ProgressDialog(loginactivity.this);
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected void onPreExecute()
- {
- super.onPreExecute();
- pdLoading.setMessage("\tLoading...");
- pdLoading.setCancelable(false);
- pdLoading.show();
- }
- @Override
- protected String doInBackground(String... params)
- {
- try
- {
- url = new URL("http://172.25.96.213/NUSVisitorWeb/login.php");
- }
- catch (MalformedURLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return "exception";
- }
- try
- {
- conn = (HttpURLConnection)url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("username", params[0])
- .appendQueryParameter("password", params[1]);
- String query = builder.build().getEncodedQuery();
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(query);
- writer.flush();
- writer.close();
- os.close();
- conn.connect();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- return "exception";
- }
- try
- {
- int response_code = conn.getResponseCode();
- if (response_code == HttpURLConnection.HTTP_OK)
- {
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null)
- {
- result.append(line);
- }
- return(result.toString());
- }
- else
- {
- return("unsuccessful");
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- return "exception";
- }
- finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result)
- {
- pdLoading.cancel();
- try
- {
- JSONObject json=new JSONObject(result);
- if(json.getString("status").equals("true"))
- {
- String fullname = json.getString("fullname");
- editor.putString("name", json.getString("fullname"));
- Toast.makeText(loginactivity.this, "Login Success...", Toast.LENGTH_LONG).show();
- Intent intent = new Intent(loginactivity.this,menuactivity.class);
- startActivity(intent);
- loginactivity.this.finish();
- }
- else if(json.getString("status").equals("false"))
- {
- Toast.makeText(loginactivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
- }
- else
- {
- Toast.makeText(loginactivity.this, "Please check server connection", Toast.LENGTH_LONG).show();
- }
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement