Guest User

Untitled

a guest
Jan 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. public class ServerRequests {
  2. ProgressDialog progressDialog;
  3. public static final int CONNECTION_TIMEOUT = 15000;
  4. public static final String SERVER_ADDRESS = "http://maynoothflag.netau.net/";
  5.  
  6. //Progress box that you can't cancel, It works while you're logging in/registering etc.
  7. public ServerRequests(Context context)
  8. {
  9. progressDialog = new ProgressDialog(context);
  10. progressDialog.setCancelable(false);
  11. progressDialog.setTitle("Processing");
  12. progressDialog.setMessage("Please wait..");
  13.  
  14. }
  15. //Store the data on the server in the background
  16. public void storeDataInBackground(Contact contact , GetUserCallback callback)
  17. {
  18. //when it is storing data in the background, our progress dialog will be shown
  19. progressDialog.show();
  20. new StoreDataAsyncTask(contact, callback).execute();
  21. }
  22.  
  23. //When the user is registering, it will call this method to post the information to the server
  24. public void fetchDataInBackground(Contact contact , GetUserCallback callback)
  25. {
  26. progressDialog.show();
  27. new FetchDataAsyncTask(contact, callback).execute();
  28. }
  29.  
  30.  
  31. public class StoreDataAsyncTask extends AsyncTask<Void , Void , Void>
  32. {
  33. Contact contact;
  34. GetUserCallback callback;
  35.  
  36. //Construcor for this new class
  37. public StoreDataAsyncTask(Contact contact , GetUserCallback callback)
  38. {
  39. this.contact = contact;
  40. this.callback = callback;
  41. }
  42.  
  43. @Override
  44. protected Void doInBackground(Void... voids) {
  45. ArrayList<NameValuePair> data_to_send = new ArrayList<>();
  46.  
  47. data_to_send.add(new BasicNameValuePair("Name" , contact.name));
  48. data_to_send.add(new BasicNameValuePair("Email" , contact.email));
  49. data_to_send.add(new BasicNameValuePair("Username" , contact.username));
  50. data_to_send.add(new BasicNameValuePair("Password" , contact.password));
  51.  
  52. HttpParams httpRequestParams = new BasicHttpParams();
  53. //Time to wait before process is executed
  54. HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
  55. //Time we want to wait to recieve something from the server
  56. HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);
  57.  
  58. //Create a new client
  59. HttpClient client = new DefaultHttpClient(httpRequestParams);
  60. //Create a new post, that will use or Register.php file (That is stored on our database)
  61. //This adds the information that the user entered in the Reigster.java file to our database
  62. HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
  63.  
  64. try {
  65. //posts the information
  66. post.setEntity(new UrlEncodedFormEntity(data_to_send));
  67. client.execute(post);
  68. }
  69. catch(Exception e)
  70. {
  71. e.printStackTrace();
  72. }
  73.  
  74. return null;
  75. }
  76.  
  77. //after our task is done, we have to dismiss the progress dialog
  78. @Override
  79. protected void onPostExecute(Void aVoid) {
  80. progressDialog.dismiss();
  81. callback.done(null);
  82.  
  83. super.onPostExecute(aVoid);
  84. }
  85. }
  86.  
  87. //Fetches users data from the database
  88. public class FetchDataAsyncTask extends AsyncTask<Void , Void , Contact>
  89. {
  90. Contact contact;
  91. GetUserCallback callback;
  92.  
  93. //Constructor for this class
  94. public FetchDataAsyncTask(Contact contact , GetUserCallback callback)
  95. {
  96. this.contact = contact;
  97. this.callback = callback;
  98. }
  99.  
  100.  
  101. @Override
  102. protected Contact doInBackground(Void... voids) {
  103. ArrayList<NameValuePair> data_to_send = new ArrayList<>();
  104. //It will only fetch the username and password, not the email (from the server)
  105. data_to_send.add(new BasicNameValuePair("Username" , contact.username));
  106. data_to_send.add(new BasicNameValuePair("Password" , contact.password));
  107.  
  108. HttpParams httpRequestParams = new BasicHttpParams();
  109. HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
  110. HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);
  111.  
  112. HttpClient client = new DefaultHttpClient(httpRequestParams);
  113. HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
  114.  
  115. Contact retunedContact = null;
  116. try {
  117. post.setEntity(new UrlEncodedFormEntity(data_to_send));
  118. HttpResponse httpResponse = client.execute(post);
  119.  
  120. HttpEntity entity = httpResponse.getEntity();
  121. String result = EntityUtils.toString(entity);
  122.  
  123.  
  124. JSONObject jsonObject = new JSONObject(result);
  125. retunedContact = null;
  126.  
  127. if(jsonObject.length() == 0)
  128. {
  129. retunedContact = null;
  130.  
  131. }
  132. else
  133. {
  134. String name,email;
  135. name = null;
  136. email=null;
  137.  
  138. if(jsonObject.has("name"))
  139. name = jsonObject.getString("name");
  140. if(jsonObject.has("email"))
  141. email =jsonObject.getString("email");
  142.  
  143. retunedContact = new Contact(name , email , contact.username , contact.password);
  144.  
  145. }
  146.  
  147. }
  148. catch(Exception e)
  149. {
  150. e.printStackTrace();
  151. }
  152.  
  153. return retunedContact;
  154. }
  155. @Override
  156. protected void onPostExecute(Contact returnedContact) {
  157. progressDialog.dismiss();
  158. callback.done(returnedContact);
  159. super.onPostExecute(returnedContact);
  160. }
  161.  
  162. }
  163. }
  164.  
  165. public class LocalDatabase {
  166. public static final String SP_NAME = "UserDetails";
  167. //Shared Preferences allows us to store data on the phone
  168. SharedPreferences localDatabase;
  169.  
  170. //A constructor for this class
  171. public LocalDatabase(Context context)
  172. {
  173. localDatabase = context.getSharedPreferences(SP_NAME , 0);
  174. }
  175.  
  176. //Storing the data
  177. public void storeData(Contact contact)
  178. {
  179. //Allows us to edit the local database
  180. SharedPreferences.Editor spEditor = localDatabase.edit();
  181. spEditor.putString("Name" , contact.name);
  182. spEditor.putString("Email" , contact.email);
  183. spEditor.putString("Username" , contact.username);
  184. spEditor.putString("Password" , contact.password);
  185. //Saves the work
  186. spEditor.commit();
  187. }
  188.  
  189. //This is for getting the data for which user is current logged in
  190. public Contact getLoggedInUser()
  191. {
  192. String name = localDatabase.getString("Name" , "");
  193. String email = localDatabase.getString("Email" , "");
  194. String username = localDatabase.getString("Username" , "");
  195. String password = localDatabase.getString("Password", "");
  196.  
  197.  
  198. //Parse your 4 variables
  199. Contact storedContact = new Contact(name , email , username , password);
  200. return storedContact;
  201. }
  202.  
  203. //Tell us if the user is currently logged in or not.. THat's why we use a boolean
  204. public void setUserLoggedIn(boolean loggedIn)
  205. {
  206. SharedPreferences.Editor spEditor = localDatabase.edit();
  207. spEditor.putBoolean("loggedIn" , loggedIn);
  208. //Save all the work
  209. spEditor.commit();
  210. }
  211.  
  212. //
  213. public boolean getUserLoggedIn()
  214. {
  215. if(localDatabase.getBoolean("loggedIn" , false))
  216. return true;
  217. else
  218. return false;
  219. }
  220.  
  221. //When the user loggs out, it will clear all the temporary data off the phone of the currently logged in user
  222. public void clearData()
  223. {
  224. //Clear all the data and save our work
  225. SharedPreferences.Editor spEditor = localDatabase.edit();
  226. spEditor.clear();
  227. spEditor.commit();
  228. }
  229.  
  230. }
  231.  
  232. public class Contact {
  233.  
  234. //Since the users id is auto incrementing in our database, we don't need to mention it here
  235. //This name, email, username and password will store the value of the below name, email, username and password
  236. String name , email , username , password;
  237. int score;
  238.  
  239. //A constructor for signing up
  240. public Contact(String name , String email , String username , String password)
  241. {
  242. this.name = name;
  243. this.email = email ;
  244. this.username = username;
  245. this.password = password;
  246. }
  247.  
  248. //A constructor for logging in
  249. public Contact(String username , String password)
  250. {
  251. this.username = username;
  252. this.password = password;
  253. }
  254.  
  255. public Contact(String username , int score)
  256. {
  257. this.username = username;
  258. this.score = score;
  259. }
  260.  
  261. }
Add Comment
Please, Sign In to add comment