Android Code Snippet Workaround the NetworkOnMainThread exception

Android 4+ doesn't allow applications to make network calls on the main thread. There is a good reason for this restriction as such calls cause the UI to freeze and after 5 second Android will show the "Application not responding" dialog.

Proper libraries take care of handling network calls with background threads. If you encounter a library that doesn't do it then you have two options:

1. Use the Threading library to start a background thread and make the calls from this thread.
2. Disable this check.

The code posted here disables this check (it only calls public APIs and is safe to use):
B4X:
Sub DisableStrictMode
   Dim jo As JavaObject
   jo.InitializeStatic("android.os.Build.VERSION")
   If jo.GetField("SDK_INT") > 9 Then
     Dim policy As JavaObject
     policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
     policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
     Dim sm As JavaObject
     sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
   End If
End Sub

Tags: NetworkOnMainThreadException, strict mode
 

Douglas Farias

Expert
Licensed User
Longtime User
i dont understand so much your english now erel.
this lib prevent the mensage "Application not responding" ?

and how to use, only put at my main and call the
DisableStrictMode

on activity create?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
this lib prevent the mensage "Application not responding" ?
No. This code prevents the NetworkOnMainThread exception (crash) which happens when you use a "poor" library that manages the network communication on the main thread.

and how to use, only put at my main and call the
DisableStrictMode
Yes. Call it once when FirstTime is True.
 

Peter Simpson

Expert
Licensed User
Longtime User
Cheers @Erel,
I've been testing the DisableStrictMode workaround out for the last couple of months. I must say that DisableStrictMode is great and I have finally been able to update 3 of my apps to Android 5.0 Lollipop because of your DisableStrictMode routine, previously I couldn't set the TargetSDK to above 9. With your routine my MySQL apps now works perfect on the latest SDKs, this means that I can finally get my apps looking modern again with my preferred MySQL Library.

Yes this DisableStrictMode has cured a lot of issues with libraries that use the main thread, so thank you...
 
Last edited:

MotoMusher

Active Member
Licensed User
Longtime User
When you say "poor" that is throwing me off. I am using http v1.36 library. I have 1 testing client getting getting android.os.networkonmainthreadexception on the httpclient call. Should I be using a different library, or are you suggesting to use the bypass above for http lib?
 
Last edited by a moderator:

Peter Simpson

Expert
Licensed User
Longtime User
Did you encounter this error? If you want to disable this check then add this code to your main activity and call DisableStrictMode.

Oops sorry @Erel, I missed this question.
Example 1. To tell you the truth on my Nexus 4, 5 and 7(2013) I never never received any errors or issues, the same for my Samsung Galaxy S4, Tab 3 10.1, HTS Desire and Acer Iconia, all works perfectly fine. But I was getting crashing issues from users saying that my widget kept stopping and hanging(it receives both XML and JSON feeds from the internet), these were mainly from 95%+ Samsung users. These issues went on for months until I found this thread not long after you originality posted it. Because I was completely lost and had no idea what to do, I blindly added the DisableStrictMode Sub routing and called it before starting anything else. I released an update to my widget onto the Play Store and all of a sudden I was getting positive user feedback saying that my widget was working perfect :). My main issue was that I have an S4 running smoothly with my widget, but I was constantly getting crash reports from S4 devices among other :confused:, but I don't remember seeing NetworkOnMainThread exception in the reports. I personally have never seen any NetworkOnMainThread exceptions, but your Java code fixed my customers issues on the Play Store.

I personally have not seen any NetworkOnMainThread exception crashed on my devices, even in the above example using the same S4 device that others that were having issues :confused:

Example 2. I faithfully use the following MySQL library and have done so for 2+ years https://www.b4x.com/android/forum/threads/mysql-library-with-jdbc.22291/#content, my only issue was that I couldn't use SDK 9+. When I set the SDK to 9+, all that would happen is that I would not receive any data from my query. Once again I had no crashes or errors, just no data would be received. I added your DisableStrictMode Sub to this library and SDK 9+ suddenly started working. Myself and my clients have never had any issues whatsoever with the GUI or anything else with this library on SDK 21 and using your DisableStrictMode Sub. Once again your DisableStrictMode worked and saved the day. Yes I already know what you think about these libraries...

Thank you @Erel :cool:
 
Last edited:
Top