Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerRequests {
- ProgressDialog progressDialog;
- public static final int CONNECTION_TIMEOUT = 15000;
- public static final String SERVER_ADDRESS = "http://maynoothflag.netau.net/";
- //Progress box that you can't cancel, It works while you're logging in/registering etc.
- public ServerRequests(Context context)
- {
- progressDialog = new ProgressDialog(context);
- progressDialog.setCancelable(false);
- progressDialog.setTitle("Processing");
- progressDialog.setMessage("Please wait..");
- }
- //Store the data on the server in the background
- public void storeDataInBackground(Contact contact , GetUserCallback callback)
- {
- //when it is storing data in the background, our progress dialog will be shown
- progressDialog.show();
- new StoreDataAsyncTask(contact, callback).execute();
- }
- //When the user is registering, it will call this method to post the information to the server
- public void fetchDataInBackground(Contact contact , GetUserCallback callback)
- {
- progressDialog.show();
- new FetchDataAsyncTask(contact, callback).execute();
- }
- public class StoreDataAsyncTask extends AsyncTask<Void , Void , Void>
- {
- Contact contact;
- GetUserCallback callback;
- //Construcor for this new class
- public StoreDataAsyncTask(Contact contact , GetUserCallback callback)
- {
- this.contact = contact;
- this.callback = callback;
- }
- @Override
- protected Void doInBackground(Void... voids) {
- ArrayList<NameValuePair> data_to_send = new ArrayList<>();
- data_to_send.add(new BasicNameValuePair("Name" , contact.name));
- data_to_send.add(new BasicNameValuePair("Email" , contact.email));
- data_to_send.add(new BasicNameValuePair("Username" , contact.username));
- data_to_send.add(new BasicNameValuePair("Password" , contact.password));
- HttpParams httpRequestParams = new BasicHttpParams();
- //Time to wait before process is executed
- HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
- //Time we want to wait to recieve something from the server
- HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);
- //Create a new client
- HttpClient client = new DefaultHttpClient(httpRequestParams);
- //Create a new post, that will use or Register.php file (That is stored on our database)
- //This adds the information that the user entered in the Reigster.java file to our database
- HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
- try {
- //posts the information
- post.setEntity(new UrlEncodedFormEntity(data_to_send));
- client.execute(post);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }
- //after our task is done, we have to dismiss the progress dialog
- @Override
- protected void onPostExecute(Void aVoid) {
- progressDialog.dismiss();
- callback.done(null);
- super.onPostExecute(aVoid);
- }
- }
- //Fetches users data from the database
- public class FetchDataAsyncTask extends AsyncTask<Void , Void , Contact>
- {
- Contact contact;
- GetUserCallback callback;
- //Constructor for this class
- public FetchDataAsyncTask(Contact contact , GetUserCallback callback)
- {
- this.contact = contact;
- this.callback = callback;
- }
- @Override
- protected Contact doInBackground(Void... voids) {
- ArrayList<NameValuePair> data_to_send = new ArrayList<>();
- //It will only fetch the username and password, not the email (from the server)
- data_to_send.add(new BasicNameValuePair("Username" , contact.username));
- data_to_send.add(new BasicNameValuePair("Password" , contact.password));
- HttpParams httpRequestParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
- HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);
- HttpClient client = new DefaultHttpClient(httpRequestParams);
- HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
- Contact retunedContact = null;
- try {
- post.setEntity(new UrlEncodedFormEntity(data_to_send));
- HttpResponse httpResponse = client.execute(post);
- HttpEntity entity = httpResponse.getEntity();
- String result = EntityUtils.toString(entity);
- JSONObject jsonObject = new JSONObject(result);
- retunedContact = null;
- if(jsonObject.length() == 0)
- {
- retunedContact = null;
- }
- else
- {
- String name,email;
- name = null;
- email=null;
- if(jsonObject.has("name"))
- name = jsonObject.getString("name");
- if(jsonObject.has("email"))
- email =jsonObject.getString("email");
- retunedContact = new Contact(name , email , contact.username , contact.password);
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return retunedContact;
- }
- @Override
- protected void onPostExecute(Contact returnedContact) {
- progressDialog.dismiss();
- callback.done(returnedContact);
- super.onPostExecute(returnedContact);
- }
- }
- }
- public class LocalDatabase {
- public static final String SP_NAME = "UserDetails";
- //Shared Preferences allows us to store data on the phone
- SharedPreferences localDatabase;
- //A constructor for this class
- public LocalDatabase(Context context)
- {
- localDatabase = context.getSharedPreferences(SP_NAME , 0);
- }
- //Storing the data
- public void storeData(Contact contact)
- {
- //Allows us to edit the local database
- SharedPreferences.Editor spEditor = localDatabase.edit();
- spEditor.putString("Name" , contact.name);
- spEditor.putString("Email" , contact.email);
- spEditor.putString("Username" , contact.username);
- spEditor.putString("Password" , contact.password);
- //Saves the work
- spEditor.commit();
- }
- //This is for getting the data for which user is current logged in
- public Contact getLoggedInUser()
- {
- String name = localDatabase.getString("Name" , "");
- String email = localDatabase.getString("Email" , "");
- String username = localDatabase.getString("Username" , "");
- String password = localDatabase.getString("Password", "");
- //Parse your 4 variables
- Contact storedContact = new Contact(name , email , username , password);
- return storedContact;
- }
- //Tell us if the user is currently logged in or not.. THat's why we use a boolean
- public void setUserLoggedIn(boolean loggedIn)
- {
- SharedPreferences.Editor spEditor = localDatabase.edit();
- spEditor.putBoolean("loggedIn" , loggedIn);
- //Save all the work
- spEditor.commit();
- }
- //
- public boolean getUserLoggedIn()
- {
- if(localDatabase.getBoolean("loggedIn" , false))
- return true;
- else
- return false;
- }
- //When the user loggs out, it will clear all the temporary data off the phone of the currently logged in user
- public void clearData()
- {
- //Clear all the data and save our work
- SharedPreferences.Editor spEditor = localDatabase.edit();
- spEditor.clear();
- spEditor.commit();
- }
- }
- public class Contact {
- //Since the users id is auto incrementing in our database, we don't need to mention it here
- //This name, email, username and password will store the value of the below name, email, username and password
- String name , email , username , password;
- int score;
- //A constructor for signing up
- public Contact(String name , String email , String username , String password)
- {
- this.name = name;
- this.email = email ;
- this.username = username;
- this.password = password;
- }
- //A constructor for logging in
- public Contact(String username , String password)
- {
- this.username = username;
- this.password = password;
- }
- public Contact(String username , int score)
- {
- this.username = username;
- this.score = score;
- }
- }
Add Comment
Please, Sign In to add comment