Android Question Inline Java question

Discussion in 'Android Questions' started by stu14t, Jul 3, 2015.

  1. stu14t

    stu14t Active Member Licensed User

    I'm Still working on the ParseUser object and I've now attempted to use Inline code.

    If the Java expert guys ever want to wrap the ParseUser for us non-speakers, that would be great. However, in the meantime, the code below is probably wrong but I hope you get the idea of what I'm attempting to achieve.

    It's an attempt at a Java subroutine which allows access to the ParseUser object which is an extension of the ParseObject but allows some security connected with creating user accouns

    I'm going to use the java object to access the sub but need to return the currentUser object and userID string from the Java routine below (if i've even come close to doing it right)

    (The ParseNative jar is an old one that works but I could replace with the current SDK)

    Here is the code :

    Code:
    #AdditionalJar: ParseNative

    #if JAVA
    import com.parse.LogInCallback;
    import com.parse.ParseException;
    import com.parse.ParseUser;

    public void parseLogin (String username,String password) {
    ParseUser.logInInBackground(username, password, new LogInCallback() {
      public void done(ParseUser user, ParseException e) {
        if (user != null) {    
         ParseUser currentUser = ParseUser.getCurrentUser();
         String userID = currentUser.getObjectId().toString();
         return currentUser, userID;
         // Hooray! The user is logged in. Return the currentUser & objectId back to B4A
        } else {
          // Signup failed. Return the ParseException to B4A to see what happened.
          return e;
         }
      }
    });
    }      

    #End if
     
  2. stu14t

    stu14t Active Member Licensed User

    Ok, I've made some progress but need some guidance. I've managed to Login (already have ParseUser account setup) and return the ObjectId of the currently logged in user. I am using this as my primary key for referencing other information about the user in other tables (or ParseObjects). This is the code below if it's of any use to anyone:

    Code:
    AdditionalJar: ParseNative

    #If JAVA
    import com.parse.ParseUser;
    public String getCurrentUserId() {

        String objectId = "";
      
        ParseUser user = ParseUser.getCurrentUser();
        if(user != null) {
            objectId = user.getObjectId();
            return objectId;
        } else {
            return "";
        }
      
    }
    #End If

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Dim Parseint As Parse
        
    Dim UserName As String
        
    Dim Password As String
        
    Dim Result As Object
      
    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

        
    Private Login As Button
        
    Private TxtUsername As EditText
        
    Private TxtPassword As EditText
        
    Private NativeMe As JavaObject
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        'Activity.LoadLayout("Layout1")
      
      
        Parseint.Initialize(
    "MyApplicationID","MyClientID")
      
        
    If FirstTime Then  
         NativeMe.InitializeContext
        
    End If 
      
        
    Activity.LoadLayout("Main")
      
      

    End Sub

    Sub ParseLogin As JavaObject
        
    Dim PL As JavaObject
        
    'Use the JavaObject to access the ParseUser
        PL.InitializeStatic("com.parse.ParseUser")
      
        
    'Run the Login Static method
        Result = PL.RunMethodJO("logIn" , Array As Object (UserName,Password))
      
        
    'Run the Inline Java to get the ObjectId of the CurrentUser  
        Dim s As String = NativeMe.RunMethodJO("getCurrentUserId"Null)  
      
        
    Log(Result)
        
    Log(s)
      
      
      
    End Sub
    Sub Activity_Resume
      

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub



    Sub Login_Click

        UserName = TxtUsername.Text
        Password = TxtPassword.Text
      
        ParseLogin  
      
    End Sub
    Now this code does return the information I need but it doesn't handle any of the ParseExceptions raised if the user credentials are incorrect or if something else goes wrong. This is not the best way to Login to the ParseUser. The documentation advises on using LogInInBackground rather than just using LogIn. The problem I have Is I don't understand how to handle the callback. I've had a think about it and I've again attempted to create an Inline Java routine that would better handle the login process although it's not quite correct. Could someone with Android Java experience have a look at the Java code below and see if I'm on the right lines?

    Code:
    import com.parse.ParseUser;
    import com.parse.ParseException;

    String objectId = "";
    public String getCurrentUserId(String userName, String passWord) {
      
      
      
        ParseUser.logInInBackground(
    String userName, String passWord, new LogInCallback() {
       
    public void done(ParseUser user, ParseException e) {
         
    if (e == null && user != null) {
           loginSuccessful();
         
    } else if (user == null) {
           usernameOrPasswordIsInvalid();
         } else {
           somethingWentWrong();
         }
       }
    });
    }
    public String loginSuccessful() {
        ParseUser user = ParseUser.getCurrentUser();
        objectId = user.getObjectId();
        return objectId;
    }
    public String usernameOrPasswordIsInvalid() {
        return "Invalid";
    }
    public String somethingWentWrong() {
        return "Error";
    }
     
    Last edited: Jul 5, 2015
Loading...