Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //error_reporting(E_ALL ^ E_DEPRECATED);
- /**
- * File to handle all API requests
- * Accepts GET and POST
- *
- * Each request will be identified by TAG
- * Response will be JSON data
- /**
- * check for POST request
- */
- if (isset($_POST['tag']) && $_POST['tag'] != '') {
- // get tag
- $tag = $_POST['tag'];
- // include DB_function
- require_once 'DB_Functions.php';
- $db = new DB_Functions();
- // response Array
- $response = array("tag" => $tag, "error" => FALSE);
- // checking tag
- if ($tag == 'login') {
- // Request type is check Login
- $mobile = $_POST['mobile'];
- $password = $_POST['password'];
- // check for user
- $user = $db->getUserByMobileAndPassword($mobile, $password);
- if ($user != false) {
- // user found
- $response["error"] = FALSE;
- $response["userID"] = $user["userID"];
- echo json_encode($response);
- } else {
- // user not found
- // echo json with error = 1
- $response["error"] = TRUE;
- $response["error_msg"] = "Incorrect mobile or password!";
- echo json_encode($response);
- }
- } else if ($tag == 'register') {
- // Request type is Register new user
- $firstname = $_POST['firstname'];
- $lastname = $_POST['lastname'];
- $mobile = $_POST['mobile']
- $email = $_POST['email'];
- $password = $_POST['password'];
- // check if user is already existed
- if ($db->isUserExisted($mobile)) {
- // user is already existed - error response
- $response["error"] = TRUE;
- $response["error_msg"] = "User already existed";
- echo json_encode($response);
- } else {
- // store user
- //Create new userID
- require_once 'userIDGenerator.php';
- $userID = createUniqueUserID($mobile);
- $user = $db->storeUser($userID,$firstname,$lastname,$mobile,$email,$password);
- if ($user) {
- // user stored successfully
- $response["error"] = FALSE;
- $response["userID"] = $user["userID"];
- echo json_encode($response);
- } else {
- // user failed to store
- $response["error"] = TRUE;
- $response["error_msg"] = "Error occured in Registartion";
- echo json_encode($response);
- }
- }
- } else {
- // user failed to store
- $response["error"] = TRUE;
- $response["error_msg"] = "Unknown 'tag' value. It should be either 'login' or 'register'";
- echo json_encode($response);
- }
- } else {
- //TODO
- echo("Server reached | Not POST Method");
- }
- ?>
- public class RegistrationActivity extends AppCompatActivity {
- //Input variables from layout
- EditText tb_firstname;
- EditText tb_lastname;
- EditText tb_mobile;
- EditText tb_email;
- EditText tb_password;
- EditText tb_confirm_password;
- CheckBox checkBox;
- TextView tv_checkBox;
- Button btn_signup;
- // Progress dialog
- private ProgressDialog pDialog;
- //Create user object from input data
- User user;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_registration);
- //Initialize layout variables
- tb_firstname = (EditText) findViewById(R.id.tb_firstname);
- tb_lastname = (EditText) findViewById(R.id.tb_last_name);
- tb_mobile = (EditText) findViewById(R.id.tb_mobile);
- tb_email = (EditText) findViewById(R.id.tb_email);
- tb_password = (EditText) findViewById(R.id.tb_password);
- tb_confirm_password = (EditText) findViewById(R.id.tb_confirm_password);
- checkBox = (CheckBox)findViewById(R.id.checkBox);
- tv_checkBox = (TextView)findViewById(R.id.tv_checkbox);
- btn_signup = (Button)findViewById(R.id.btn_signup);
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- //Add necessary validation after input from user
- tb_firstname.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void afterTextChanged(Editable editable) {
- String firstName = tb_firstname.getText().toString();
- if(!firstName.isEmpty()){
- if(!isAlpha(firstName)){
- // contains a number
- //tb_firstname.setText("");
- tb_firstname.setError("Alphabets allowed only");
- }
- }
- }
- });
- tb_lastname.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void afterTextChanged(Editable editable) {
- String lastName = tb_lastname.getText().toString();
- if(!lastName.isEmpty()){
- if(!isAlpha(lastName)){
- // contains a number
- //tb_lastname.setText("");
- tb_lastname.setError("Alphabets allowed only");
- }
- }
- }
- });
- tb_mobile.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- String mobile = tb_mobile.getText().toString();
- if(!mobile.isEmpty()){
- if(!isValidMobile(mobile)){
- // contains a number
- //tb_mobile.setText("");
- tb_mobile.setError("Invalid");
- }
- }
- }
- @Override
- public void afterTextChanged(Editable editable) {
- }
- });
- tb_email.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void afterTextChanged(Editable editable) {
- String email = tb_email.getText().toString();
- if(!email.isEmpty()){
- if(!isValidEmail(email)){
- // contains a number
- // tb_email.setText("");
- tb_email.setError("Invalid");
- }
- }
- }
- });
- tb_confirm_password.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void afterTextChanged(Editable editable) {
- String pass = tb_password.getText().toString();
- String confirmed_pass = tb_confirm_password.getText().toString();
- if(!pass.isEmpty() && !confirmed_pass.isEmpty()){
- if(!pass.contentEquals(confirmed_pass)){
- // contains a number
- tb_confirm_password.setError("Passwords Don't Match!");
- }
- }
- }
- });
- tv_checkBox.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(view == tv_checkBox){
- //Display t&C in dialog fragment
- }
- }
- });
- btn_signup.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(view == btn_signup){
- if(checkBox.isChecked()) {
- //Register User
- //Store user inputs in user variable
- user = new User();
- user.setFirstname(tb_firstname.getText().toString());
- user.setLastname(tb_lastname.getText().toString());
- user.setMobile(tb_mobile.getText().toString());
- user.setEmail(tb_email.getText().toString());
- user.setPassword(tb_password.getText().toString());
- if(isConnectedToInternet(RegistrationActivity.this)) {
- Register(user);
- }else{
- //Toast : "Network not available"
- Toast toast=Toast.makeText(getApplicationContext(),"Network not available", Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- else{
- //Toast : "Terms and conditions not agreed"
- Toast toast=Toast.makeText(getApplicationContext(),"Terms and conditions not agreed", Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- }
- });
- }
- private void Register(final User user) {
- // Tag used to cancel the request
- String tag_string_req = "req_register";
- pDialog.setMessage("Registering ...");
- showDialog();
- StringRequest strReq = new StringRequest(Request.Method.POST,
- "http://paaniman.com/android_locker/userHandlerApi.php", new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- if (!error) {
- pDialog.setMessage("Initializing ...");
- showDialog();
- //Store data locally
- SharedPreferences pref = getApplicationContext().getSharedPreferences(getResources().getString(R.string.SP_app_data), MODE_PRIVATE);
- SharedPreferences.Editor editor = pref.edit();
- editor.putBoolean(getResources().getString(R.string.app_user_registered_flag),true);
- editor.apply();
- pref = getApplicationContext().getSharedPreferences(getResources().getString(R.string.SP_user_data), MODE_PRIVATE);
- editor = pref.edit();
- editor.putString(getString(R.string.user_userID),jObj.getString("userID"));
- editor.putString(getString(R.string.user_first_name),user.getFirstname());
- editor.putString(getString(R.string.user_last_name),user.getLastname());
- editor.putString(getString(R.string.user_mobile),user.getMobile());
- editor.putString(getString(R.string.user_email),user.getEmail());
- editor.putString(getString(R.string.user_password),user.getPassword());
- editor.apply();
- Thread timer= new Thread()
- {
- public void run()
- {
- try
- {
- //Display for 3 seconds
- sleep(2000);
- hideDialog();
- }
- catch (InterruptedException e)
- {
- // TODO: handle exception
- e.printStackTrace();
- }
- finally
- {
- Intent intent = new Intent(
- RegistrationActivity.this,
- LocationActivity.class);
- startActivity(intent);
- finish();
- }
- }
- };
- timer.start();
- } else {
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting params to register url
- Map<String, String> params = new HashMap<String, String>();
- params.put("tag", "register");
- params.put("firstname", user.getFirstname());
- params.put("lastname", user.getLastname());
- params.put("mobile",user.getMobile());
- params.put("email",user.getEmail());
- params.put("password", user.getPassword());
- return params;
- }
- };
- AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
- }
- @Override
- protected void onResume(){
- super.onResume();
- }
- @Override
- protected void onPause(){
- super.onPause();
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
- public boolean isAlpha(String name) {
- return name.matches("[a-zA-Z]+");
- }
- private boolean isValidMobile(String phone)
- {
- if(phone.length()==10) {
- return true;
- }else{
- return false;
- }
- }
- private boolean isValidEmail(String email)
- {
- return Patterns.EMAIL_ADDRESS.matcher(email).matches();
- }
- public static boolean isConnectedToInternet(Context context)
- {
- // Check intenet connectivity
- boolean connected = false;
- try
- {
- ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
- connected = ( conMgr.getActiveNetworkInfo() != null &&
- conMgr.getActiveNetworkInfo().isAvailable() &&
- conMgr.getActiveNetworkInfo().isConnected() );
- }catch (Exception e)
- {
- return false;
- }
- return connected;
- }
- }
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: org.json.JSONException: End of input at character 0 of
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.chaoslabs.paaniman.RegistrationActivity$8.onResponse(RegistrationActivity.java:252)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.chaoslabs.paaniman.RegistrationActivity$8.onResponse(RegistrationActivity.java:245)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.os.Looper.loop(Looper.java:135)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5237)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at java.lang.reflect.Method.invoke(Native Method)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
- 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement