Android Code Snippet Workaround the NetworkOnMainThread exception

Discussion in 'Code Snippets' started by Erel, Sep 16, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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):
    Code:
    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
     
    HectorPuk, Mashiane, KZero and 8 others like this.
  2. Douglas Farias

    Douglas Farias Expert Licensed 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?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    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.

    Yes. Call it once when FirstTime is True.
     
    Douglas Farias likes this.
  4. Peter Simpson

    Peter Simpson Expert Licensed 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: Jan 1, 2015
  5. Erel

    Erel Administrator Staff Member Licensed User

    Did you encounter this error? If you want to disable this check then add this code to your main activity and call DisableStrictMode.
     
  6. ibonk64

    ibonk64 Member Licensed User

  7. Erel

    Erel Administrator Staff Member Licensed User

    Please do not delete your questions next time. It makes the threads more difficult to read.
     
    Peter Simpson likes this.
  8. MotoMusher

    MotoMusher Active Member Licensed 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?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    You should definitely not use this solution. The proper solution is to use HttpUtils2. Though you can also use HttpClient however make sure not to call Response.GetString.
     
    Mikonios likes this.
  10. Peter Simpson

    Peter Simpson Expert Licensed User

    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 http://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: Jan 26, 2015
  11. MotoMusher

    MotoMusher Active Member Licensed User

    My issue was isolated to a single S4 user as well. Other S4 users were fine, as was my S3 and S5.
     
    Peter Simpson likes this.
  12. Bryanne Vega

    Bryanne Vega Member Licensed User

    This is happening on the SSLSocket library
     
  13. touchvip

    touchvip Member Licensed User

    Just solve my big problem
     
Loading...