Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class RegistrationTask extends AsyncTask<Void, Void, Boolean> {
- private final String TAG = "RegistrationTask: ";
- private final String email;
- private final String password;
- private boolean success;
- private StringBuffer response;
- RegistrationTask(String email, String password) {
- this.email = email;
- this.password = password;
- }
- @Override
- protected Boolean doInBackground(Void... params) {
- try {
- Map<String, String> paramdata = new HashMap<>();
- paramdata.put("tag", "register");
- paramdata.put("email", email);
- paramdata.put("password", password);
- String registerUrl = "http://marshalllee.netne.net/LoginModule/index.php";
- URL url = new URL(registerUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setReadTimeout(10000);
- connection.setConnectTimeout(15000);
- connection.setRequestMethod("POST");
- connection.setDoOutput(true);
- StringBuffer requestParams = new StringBuffer();
- if(paramdata != null && paramdata.size() > 0) {
- Iterator<String> paramIterator = paramdata.keySet().iterator();
- while(paramIterator.hasNext()) {
- String key = paramIterator.next();
- String value = paramdata.get(key);
- requestParams.append(URLEncoder.encode(key, "UTF-8"));
- requestParams.append("=").append(URLEncoder.encode(value, "UTF-8"));
- requestParams.append("&");
- }
- // send POST request
- OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
- writer.write(requestParams.toString());
- Log.v(TAG, requestParams.toString());
- writer.flush();
- }
- // get the response from the server.
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String line = null;
- response = new StringBuffer();
- while((line = in.readLine()) != null) {
- response.append(line);
- }
- success = true;
- } catch(IOException e) {
- Log.v(TAG, e.getMessage());
- success = false;
- }
- return success;
- }
- @Override
- protected void onPostExecute(Boolean aBoolean) {
- super.onPostExecute(aBoolean);
- if(aBoolean) {
- Log.v(TAG, "success");
- } else {
- Log.v(TAG, "failure");
- }
- }
- }
- 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 -->
- This is the corresponding PHP code.
- <?php
- require_once 'db/db_functions.php';
- $db = new DB_Functions();
- if (isset($_POST['tag']) && !empty($_POST['tag'])) {
- $tag = $_POST['tag'];
- // get the variables.
- $email = $_POST['email'];
- $password = $_POST['password'];
- // response Array
- $response = array("error" => FALSE);
- // check for tag type
- if ($tag == 'login') {
- // check for user
- $user = $db->getUserByEmailAndPassword($email, $password);
- if ($user != false) {
- // user found
- $response["error"] = FALSE;
- $response["uid"] = $user["unique_id"];
- $response["user"]["email"] = $user["email"];
- $response["user"]["created_at"] = $user["created_at"];
- $response["user"]["updated_at"] = $user["updated_at"];
- echo json_encode($response);
- } else {
- // user not found
- // echo json with error = 1
- $response["error"] = TRUE;
- $response["error_msg"] = "Incorrect email or password!";
- echo json_encode($response);
- }
- } else if ($tag == 'register') {
- // check if user is already exists
- if ($db->userExists($email)) {
- // user already exists - error response
- $response["error"] = TRUE;
- $response["error_msg"] = "User already exists";
- echo json_encode($response);
- } else {
- // store user
- $user = $db->storeUser($email, $password);
- if ($user != false) {
- // user successfully saved to MySQL database
- $response["error"] = FALSE;
- $response["uid"] = $user["unique_id"];
- $response["user"]["email"] = $user["email"];
- $response["user"]["created_at"] = $user["created_at"];
- $response["user"]["updated_at"] = $user["updated_at"];
- echo json_encode($response);
- } else {
- // user failed to store
- $response["error"] = TRUE;
- $response["error_msg"] = "JSON Error occured in Registration";
- echo json_encode($response);
- }
- }
- } else {
- // user failed to store
- $response["error"] = TRUE;
- $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
- echo json_encode($response);
- }
- } else {
- $response["error"] = TRUE;
- $response["error_msg"] = "Operation failed due to the missing tag!";
- echo json_encode($response);
- }
- var_dump($_SERVER['REQUEST_METHOD'], $_POST);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement