1. Are you ready for the Galaxy S20? Here is everything we know so far!

[Networking]Run callback on main thread

Discussion in 'Android Development' started by Bruud, Sep 7, 2020.

  1. Bruud

    Bruud Lurker
    Thread Starter

    I'm developing an app where I need to have a login screen, so users can login.
    At the moment I have the login completely working.
    The way I'm doing it is as follows
    • The user presses the login button
    • A second thread (Networking thread) is connecting to a webpage with username and password as the post variables
    • The page then checks if the username and password match with a row in my accounts table
    • The page then outputs a 0 (if successful) a 1 (if not successful)
    • The networking thread reads the result and calls a callback with the result
    • The callback then is called on the ui thread of the app and displays a toast if the login wasn't successful or continues to the next activity
    Now comes the part where I'm a bit more unsure about, which is the code. To me it feels like overkill to do it this way (especially the part where I put the calling activity as a variable for the callback to execute the callback on the ui thread).

    Login method in the login activity
    Code (Java):
    1.  
    2. private void login() {
    3.     Login login = new Login(this);
    4.     login.makeRequest(new Callback<String>() {
    5.         @Override
    6.         public void onComplete(Result<String> result) {
    7.             if (result instanceof Result.Success) {
    8.                 Intent dsp = new Intent(LoginActivity.this, MainActivity.class);
    9.                 startActivity(dsp);
    10.             } else {
    11.                 Result.Error<String> error = (Result.Error<String>) result;
    12.                 Toast.makeText(getApplicationContext(), error.message, Toast.LENGTH_SHORT).show();
    13.             }
    14.         }
    15.     }, usernameEt.getText().toString(), passwordEt.getText().toString());
    16. }
    17.  
    makeRequest method in the Login class
    Code (Java):
    1.  
    2. public void makeRequest(final Callback<String> callback, final String username, final String password) {
    3.     new Thread(new Runnable() {
    4.         @Override
    5.         public void run() {
    6.             final Result<String> result = makeSynchronousRequest(username, password);
    7.             callback.execute(result, callingActivity);
    8.         }
    9.     }).start();
    10. }
    11.  
    Callback class
    Code (Java):
    1.  
    2. public abstract class Callback<T> {
    3.  
    4.     public abstract void onComplete(Result<T> result);
    5.  
    6.     public void execute(final Result<T> result, AppCompatActivity callingActivity) {
    7.         callingActivity.runOnUiThread(new Runnable() {
    8.             @Override
    9.             public void run() {
    10.                 onComplete(result);
    11.             }
    12.         });
    13.     }
    14. }
    15.  
     


Loading...

Share This Page

Loading...