Jump to content

Welcome! You're currently a Guest.

If you'd like to join in the Discussion, or access additional features in our forums, please sign in with your Evernote Account here. Have an Evernote Account but forgot your password? Reset it! Don't have an account yet? Create One! You'll need to set your Display Name before your first post.

Photo

Android oAuth service throws NetworkOnMainThreadException

android oauth

  • Please log in to reply
4 replies to this topic

#1 Martin Harvey

Martin Harvey

  • Pip
  • Title: Member
  • Group: Members
  • 12 posts

Posted 28 June 2012 - 04:01 PM

I have only just encountered this so was wondering if anyone else had. The Android oAuth library starts another activity and that activity causes a NetworkOnMainThreadException. Apparently network access on the UI thread used to be allowed, but starting with Honeycomb this exception is thrown. As far as I can tell at the moment, the Android oAuth library will not work on Honeycomb (and probably above).

However, I had to create a local copy of the oAuth library for another reason, so I could be missing a fix. Am I?

#2 Martin Harvey

Martin Harvey

  • Pip
  • Title: Member
  • Group: Members
  • 12 posts

Posted 28 June 2012 - 05:05 PM

Turns out that, something called StrictMode was introduced in Gingerbread and turned off by default. However, in HoneyComb 'StrictMode' is turned on. This prevents network access on the UI thread. This allows the following workaround (discouraged obviously)...

In class EvernoteOAuthActivity (package: com.evernote.client.oauth.android of the oAuth library) add the following lines in the onCreate method:

public void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

You could presumably use permitNetwork() instead of permitAll(). I am running with this until a permanent fix is available. Since this is only invoked when authenticating (which could be as little as once every 365 days) I don't think it is a problem.

Maybe I will get my devCup submission in on time after all...!

#3 Martin Harvey

Martin Harvey

  • Pip
  • Title: Member
  • Group: Members
  • 12 posts

Posted 29 June 2012 - 02:08 PM

FYI: The above fix does not support Froyo so I used the method shown here to implement version dependent code.

A (probably should be abstract) class implements the permitAll() method...
public class VersionedStrictPolicy {
    VersionedStrictPolicy() {
    }

    public void permitAll() {
    }
}

The Froyo implementation does nothing (in the end it is not needed)...
public class FroyoStrictPolicy extends VersionedStrictPolicy {

}

The Honeycomb implementation disables strict mode...
public class HoneycombStrictPolicy extends VersionedStrictPolicy{

    private static final String TAG = "HoneycombStrictPolicy";
    
    public void permitAll () {
        Log.i(TAG, "Adjusting StrictMode for authorization...");
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
}

The EvernoteOAuthActivity then instantiates the appropriate class depending on the version:
public void onCreate(Bundle savedInstanceState) {
      final int androidVersion = Build.VERSION.SDK_INT;
      if (androidVersion > Build.VERSION_CODES.GINGERBREAD_MR1) {
          VersionedStrictPolicy strictPolicy = new HoneycombStrictPolicy();
          strictPolicy.permitAll();
      } else {
          // we could instantiate the FroyoStrictPolicy, but it is a no-op anyway.
      }
    ...


#4 SethH

SethH

  • Title: Evernote Employee
  • Group: Evernote Employee
  • 683 posts

Posted 10 July 2012 - 04:34 AM

Sorry about the PITA here, we're aware of this and will be resolving it in an upcoming SDK release.

#5 kohara

kohara

  • Pip
  • Title: Member
  • Group: Members
  • 1 posts

Posted 09 August 2012 - 09:41 AM

I am also having trouble with the same issue.
Can you please tell me exactly when the issue will be resolved.
When do you plan to release the modified version of SDK?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

Clip to Evernote