BackEndless.Com - Asynchronous Login in Fragment

219 Views Asked by At

I am using backendless.com as my backend for my application. I need to log my user in within a fragment. I keep getting a syntax error stating that the Asynchronous login method provided by backendless.com is not recognized. It works perfectly fine within an Activity. Does anyone know how to get it to work within a Fragment? Here is a screenshot of the error: enter image description here

Here is the code for my Fragment:

public class LoginFragment extends Fragment implements View.OnClickListener {
private FragmentTransaction ft;
private Button registerButton, resetButton, loginButton;
EditText userName, password;
private boolean isPopUpOpen;
BackendlessUser userOne = new BackendlessUser();
private static final String PREFS_LOGGED_IN = "AreYouLoggedInFile";

public OnClickedListener listener;
public LogInInterface loggedInListener;

static interface OnClickedListener{
    public void buttonClicked(View v);
}

static interface LogInInterface{
    public void userLoggedIn(boolean loggedIn);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.listener = (OnClickedListener)activity;
    this.loggedInListener = (LogInInterface)activity;
}

public LoginFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    isPopUpOpen = false;
    if (savedInstanceState!=null){
        if (savedInstanceState.getBoolean("isDialogOpen")){
            resetPopUpWindow();
        }
    }
    View view = inflater.inflate(R.layout.fragment_login, container, false);
    registerButton = (Button)view.findViewById(R.id.register_button);
    resetButton = (Button) view.findViewById(R.id.reset_button);
    password = (EditText)view.findViewById(R.id.fragment_login_password);
    userName = (EditText)view.findViewById(R.id.fragment_login_username);

    loginButton = (Button)view.findViewById(R.id.fragment_login_loginButton);
    registerButton.setOnClickListener(this);
    resetButton.setOnClickListener(this);
    loginButton.setOnClickListener(this);
    return view;
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.register_button:{
            listener.buttonClicked(v);
            break;
        }
        case R.id.reset_button:{
            isPopUpOpen = true;
            resetPopUpWindow();
            break;
        }
        case R.id.fragment_login_loginButton:{
            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Logging In...");
            progressDialog.show();//FOLLOWING METHOD NOT WORKING...



            Backendless.UserService.login("email", password, new AsyncCallback<BackendlessUser>() {
                public void handleResponse(BackendlessUser user) {
                    Toast.makeText(getActivity(), "Logged In!", Toast.LENGTH_LONG).show();
                    SharedPreferences myPrefs = getActivity().getSharedPreferences(PREFS_LOGGED_IN, 0);
                    SharedPreferences.Editor editor = myPrefs.edit();
                    editor.putBoolean("isLoggedIn", true);
                    editor.commit();

                }

                public void handleFault(BackendlessFault fault) {
                    Toast.makeText(getActivity(), "No Name", Toast.LENGTH_LONG).show();
                }
            });


        }
        break;
    }
2

There are 2 best solutions below

1
On BEST ANSWER

The login() method for Backendless expects a String as the second parameter. You're passing it an EditText. You have to extract the value out of the EditText. Do password.getText().toString() as your second parameter to the login() method.

And, its a good practice to check for empty value in the EditText before you actually send the value. So make sure to do those checks.

0
On

As this website mentions the login method syntax is like this:

public void Backendless.UserService.login( String login, 
                                          String password, 
                                          AsyncCallback<BackendlessUser> callback );

public void Backendless.UserService.login( String login, 
                                          String password, 
                                          boolean stayLoggedIn, 
                                          AsyncCallback<BackendlessUser> callback );

and you are passing an EditText instead of a String. So replace this:

Backendless.UserService.login("email", password, new AsyncCallback<BackendlessUser>() {
                public void handleResponse(BackendlessUser user) {
                    Toast.makeText(getActivity(), "Logged In!", Toast.LENGTH_LONG).show();
                    SharedPreferences myPrefs = getActivity().getSharedPreferences(PREFS_LOGGED_IN, 0);
                    SharedPreferences.Editor editor = myPrefs.edit();
                    editor.putBoolean("isLoggedIn", true);
                    editor.commit();

                }

                public void handleFault(BackendlessFault fault) {
                    Toast.makeText(getActivity(), "No Name", Toast.LENGTH_LONG).show();
                }
            });

with this:

Backendless.UserService.login("email", password.getText().toString(), new AsyncCallback<BackendlessUser>() {
                public void handleResponse(BackendlessUser user) {
                    Toast.makeText(getActivity(), "Logged In!", Toast.LENGTH_LONG).show();
                    SharedPreferences myPrefs = getActivity().getSharedPreferences(PREFS_LOGGED_IN, 0);
                    SharedPreferences.Editor editor = myPrefs.edit();
                    editor.putBoolean("isLoggedIn", true);
                    editor.commit();

                }

                public void handleFault(BackendlessFault fault) {
                    Toast.makeText(getActivity(), "No Name", Toast.LENGTH_LONG).show();
                }
            });

Hope it helps!!!