Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. private class RegistrationTask extends AsyncTask<Void, Void, Boolean> {
  2. private final String TAG = "RegistrationTask: ";
  3. private final String email;
  4. private final String password;
  5. private boolean success;
  6. private StringBuffer response;
  7.  
  8. RegistrationTask(String email, String password) {
  9. this.email = email;
  10. this.password = password;
  11. }
  12.  
  13. @Override
  14. protected Boolean doInBackground(Void... params) {
  15. try {
  16. Map<String, String> paramdata = new HashMap<>();
  17. paramdata.put("tag", "register");
  18. paramdata.put("email", email);
  19. paramdata.put("password", password);
  20.  
  21. String registerUrl = "http://marshalllee.netne.net/LoginModule/index.php";
  22. URL url = new URL(registerUrl);
  23. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  24. connection.setReadTimeout(10000);
  25. connection.setConnectTimeout(15000);
  26. connection.setRequestMethod("POST");
  27. connection.setDoOutput(true);
  28.  
  29. StringBuffer requestParams = new StringBuffer();
  30.  
  31. if(paramdata != null && paramdata.size() > 0) {
  32. Iterator<String> paramIterator = paramdata.keySet().iterator();
  33. while(paramIterator.hasNext()) {
  34. String key = paramIterator.next();
  35. String value = paramdata.get(key);
  36. requestParams.append(URLEncoder.encode(key, "UTF-8"));
  37. requestParams.append("=").append(URLEncoder.encode(value, "UTF-8"));
  38. requestParams.append("&");
  39. }
  40.  
  41. // send POST request
  42. OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
  43. writer.write(requestParams.toString());
  44. Log.v(TAG, requestParams.toString());
  45. writer.flush();
  46. }
  47.  
  48. // get the response from the server.
  49.  
  50. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  51. String line = null;
  52. response = new StringBuffer();
  53. while((line = in.readLine()) != null) {
  54. response.append(line);
  55.  
  56. }
  57.  
  58. success = true;
  59.  
  60. } catch(IOException e) {
  61. Log.v(TAG, e.getMessage());
  62. success = false;
  63. }
  64. return success;
  65. }
  66.  
  67. @Override
  68. protected void onPostExecute(Boolean aBoolean) {
  69. super.onPostExecute(aBoolean);
  70. if(aBoolean) {
  71. Log.v(TAG, "success");
  72. } else {
  73. Log.v(TAG, "failure");
  74. }
  75. }
  76.  
  77. }
  78.  
  79. 01-11 21:26:53.336 6655-7021/? V/RegistrationTask:: Response: {"error":true,"error_msg":"User already exists"}string(4) "POST"array(3) { ["password"]=> string(8) "********" ["email"]=> string(24) "********@gmail.com" ["tag"]=> string(8) "register"}<!-- Hosting24 Analytics Code --><script type="text/javascript" src="http://stats.hosting24.com/count.php"></script><!-- End Of Analytics Code -->
  80.  
  81. This is the corresponding PHP code.
  82.  
  83. <?php
  84.  
  85. require_once 'db/db_functions.php';
  86. $db = new DB_Functions();
  87.  
  88. if (isset($_POST['tag']) && !empty($_POST['tag'])) {
  89.  
  90. $tag = $_POST['tag'];
  91.  
  92. // get the variables.
  93. $email = $_POST['email'];
  94. $password = $_POST['password'];
  95.  
  96. // response Array
  97. $response = array("error" => FALSE);
  98.  
  99. // check for tag type
  100. if ($tag == 'login') {
  101.  
  102. // check for user
  103. $user = $db->getUserByEmailAndPassword($email, $password);
  104. if ($user != false) {
  105.  
  106. // user found
  107. $response["error"] = FALSE;
  108. $response["uid"] = $user["unique_id"];
  109. $response["user"]["email"] = $user["email"];
  110. $response["user"]["created_at"] = $user["created_at"];
  111. $response["user"]["updated_at"] = $user["updated_at"];
  112.  
  113. echo json_encode($response);
  114. } else {
  115. // user not found
  116. // echo json with error = 1
  117. $response["error"] = TRUE;
  118. $response["error_msg"] = "Incorrect email or password!";
  119. echo json_encode($response);
  120. }
  121. } else if ($tag == 'register') {
  122.  
  123. // check if user is already exists
  124.  
  125. if ($db->userExists($email)) {
  126.  
  127. // user already exists - error response
  128. $response["error"] = TRUE;
  129. $response["error_msg"] = "User already exists";
  130. echo json_encode($response);
  131.  
  132. } else {
  133.  
  134. // store user
  135. $user = $db->storeUser($email, $password);
  136.  
  137.  
  138. if ($user != false) {
  139. // user successfully saved to MySQL database
  140. $response["error"] = FALSE;
  141. $response["uid"] = $user["unique_id"];
  142. $response["user"]["email"] = $user["email"];
  143. $response["user"]["created_at"] = $user["created_at"];
  144. $response["user"]["updated_at"] = $user["updated_at"];
  145. echo json_encode($response);
  146.  
  147. } else {
  148. // user failed to store
  149. $response["error"] = TRUE;
  150. $response["error_msg"] = "JSON Error occured in Registration";
  151. echo json_encode($response);
  152. }
  153. }
  154. } else {
  155. // user failed to store
  156. $response["error"] = TRUE;
  157. $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
  158. echo json_encode($response);
  159. }
  160. } else {
  161.  
  162. $response["error"] = TRUE;
  163. $response["error_msg"] = "Operation failed due to the missing tag!";
  164. echo json_encode($response);
  165. }
  166.  
  167.  
  168. var_dump($_SERVER['REQUEST_METHOD'], $_POST);
  169. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement