Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nus.app1;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- 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
- {
- Button btnlogin;
- EditText tusername,tpassword;
- public static final int CONNECTION_TIMEOUT = 10000;
- public static final int READ_TIMEOUT = 150000;
- SharedPreferences sharedpreferences;
- public static final String serverpref = "serverpref";
- public static final String serveraddress = "serveraddress";
- private String saddress;
- private String url_login;
- public void onCreate(Bundle b)
- {
- super.onCreate(b);
- setContentView(R.layout.loginlayout);
- btnlogin=findViewById(R.id.btnlogin);
- tusername=findViewById(R.id.tusername);
- tpassword=findViewById(R.id.tpassword);
- sharedpreferences = getSharedPreferences(serverpref, Context.MODE_PRIVATE);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(tusername.getText().length()==0 || tpassword.getText().length()==0)
- {
- Toast.makeText(getApplicationContext(),"Username or Password is empty",Toast.LENGTH_LONG).show();
- }
- else
- {
- if (!sharedpreferences.contains(serveraddress) || sharedpreferences.getString(serveraddress,null)== null || sharedpreferences.getString(serveraddress,null)=="" )
- {
- Toast.makeText(getApplicationContext(),"Please set your server address here",Toast.LENGTH_LONG).show();
- Intent serveraddressintent=new Intent(getApplicationContext(),ServerAddressActivity.class);
- startActivity(serveraddressintent);
- }
- else
- {
- saddress=sharedpreferences.getString(serveraddress, "");
- url_login="http://"+saddress+"/servercode/loginprocess.php";
- new AsyncLogin().execute(tusername.getText().toString(), tpassword.getText().toString());
- }
- }
- }
- });
- }
- public boolean onCreateOptionsMenu(Menu menu)
- {
- super.onCreateOptionsMenu(menu);
- MenuInflater menuInflater=getMenuInflater();
- menuInflater.inflate(R.menu.mymenu,menu);
- return true;
- }
- public boolean onOptionsItemSelected(MenuItem menuItem)
- {
- switch (menuItem.getItemId())
- {
- case R.id.serveraddress:
- Intent intent=new Intent(getApplicationContext(),ServerAddressActivity.class);
- startActivity(intent);
- return true;
- default:
- return super.onContextItemSelected(menuItem);
- }
- }
- 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(url_login);
- }
- 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("fullname", 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