trishan/posit-mobile

NetworkOnMainThreadException in authenticator

Opened this issue · 4 comments

What steps will reproduce the problem?
1. Clean the project.
2. Set targetSDK to be 18 or 19
3. Launch the app

What is the expected output? What do you see instead?
The app display an error message "Sorry, the server is not reachable", even 
though the server is just running fine. With some care, I found that 
Communicator.doHTTPGET() method throws NetworkOnMainThreadException if run on 
API level exceeding 10.

It turns out that Honeycomb and later versions of Android prohibits any network 
operation on the main thread:
http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

Fix: offload network operation to a worker thread.

Original issue reported on code.google.com by Hyunsu.Cho.2015@trincoll.edu on 13 Apr 2014 at 1:37

Original comment by Hyunsu.Cho.2015@trincoll.edu on 13 Apr 2014 at 1:39

  • Added labels: Priority-Critical
  • Removed labels: Priority-High
There are lots of places where doHTTPGET() is referenced; so doing it 
asynchronously would require revamping lots of code.

Original comment by Hyunsu.Cho.2015@trincoll.edu on 13 Apr 2014 at 3:43

I just found out that AccountManager.blockingGetAuthToken() suffers from the 
same problem. I think this is related to Issue 361 
(https://code.google.com/p/posit-mobile/issues/detail?id=361). The difference 
is that, now with API 10 or higher, Android is very strict about separation 
between UI thread and worker thread.

Original comment by Hyunsu.Cho.2015@trincoll.edu on 13 Apr 2014 at 3:48

Sounds like a good, focused project for someone!  Thanks for posting this.

Original comment by ram8...@gmail.com on 13 Apr 2014 at 11:57