Async auth off the main thread & error checking

59 views
Skip to first unread message

brandall

unread,
Aug 31, 2012, 5:48:53 PM8/31/12
to jt...@googlegroups.com
Firstly, thanks for this great library. It's taken me only a short time to get everything up and running which hasn't been my experience with other twitter libraries! It's also very light weight, which is great. I've a few questions regarding my implementation:

From my main activity, I have an 'authorise twitter button' which onClick:

AndroidTwitterLogin atl = new AndroidTwitterLogin(this,
"this_is_populated",
"so_is_this",

protected void onSuccess(Twitter jtwitter, String[] tokens) {
twitterStoreKeys(tokens[0], tokens[1]); // my store in shared prefs method
}

I have to use StrictMode though for network on the main thread. I've looked at Async'ing parts of the AndroidTwitterLogin class, but I can't see a way to do it and handle the webView. I've trawled Stackoverflow, but I think this is a relatively new class you've introduced, so can't find any examples. How do you envisage it being used from a button click like this?

Once I have the tokens stored in shared preferences, I can successfully tweet without reauthorising, but I wasn't sure how and where to check for errors during this process?

This is my implementation from an AsyncTask to post a tweet:

               static Twitter twitter;

               // other code here

               // tokens are retrieved from shared preferences

if (tokenKey != null && tokenSecret != null) {

OAuthSignpostClient ouathClient = new OAuthSignpostClient(
"this_is_populated",
"so_is_this", tokenKey,
tokenSecret);

twitter = new Twitter(null, ouathClient);

if (twitter.isValidLogin()) {
try {
twitter.setStatus("blar blar");

} catch (TwitterException e) {
e.printStackTrace();
                                        // denotes a twitter/network failure
}

} else {
// user has revoked permissions?
}

} else {
// My keys are broken
}

Is my code above performing the correct checks and catching the right errors? Is there anything you'd suggest to catch more specific failures?

Thanks in advance for your guidance.

Daniel Winterstein

unread,
Sep 6, 2012, 7:45:24 AM9/6/12
to jt...@googlegroups.com

Hi Brandall,

This is just a quick off-the-cuff reply, as it's a manic time at work.

AndroidTwitterLogin needs control of the UI, so I don't think you can async it.
Happy to be proved wrong!

But once you've got authorisation tokens from it, you can save them & reuse them forever.

Here's an example of using AndroidTwitterLogin:

final AndroidTwitterLogin atl = new AndroidTwitterLogin(app,
                TWITTER_KEY,TWITTER_SECRET,CALLBACK) {                   
            @Override

            protected void onSuccess(Twitter jtwitter, String[] tokens) {
               // Save the tokens in your app for future use!
               // Do stuff now if you like, e.g.
               jtwitter.setStatus("Hello World");
            }
            protected void onFail(Exception e) {
               // oh well
            }
        };
        // Now make your "authorise twitter" button
        myButton.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {               
                myButton.setEnabled(false);
                // Let's add a tiny pause to let myButton update to look disabled
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        atl.run();
                    }
                },5);
            }
        });

I hope that helps.

Best regards,
 - Daniel

brandall

unread,
Sep 7, 2012, 7:01:21 AM9/7/12
to jt...@googlegroups.com
Thanks for your reply. If I apply your exact code, it fails with a network on the main thread error. I've had a good search on Stackoverflow regarding this issue and to be honest, I remain completely confused! It appears as though in lower API levels, there is no issue, but that changed from Honeycomb onwards. In order to get the code to work I have to include in onCreate:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);

This option is only available from API level 9.

From what I can understand (vaguely), the network on main thread is just a warning that needs to be acknowledged by inserting the above code... It seems irrelevant when using webView though, which has no choice but to be on the main thread!?

Anyway, as my app targets API 9+, all is well...

Cheers

Ben 
Reply all
Reply to author
Forward
0 new messages