Android Question NetworkOnMainThreadException on 3G data, not on WiFI

adrianstanescu85

Active Member
Licensed User
Longtime User
Hello all,

When my B4A app starts, it verifies the size of a certain file on the internet, so if a certain limit is reached then that file is downloaded. This works just fine when my device gets its internet access from my WiFi network, but when I get out of the house and I run the app with the mobile data plan, I get an android.os error saying NetworkOnMainThreadException.

I set the internet premission in the Android manifest since a few forum posts mentioned this but it doesn't seem to help, actually the app works fine without it but only when I get the internet by WiFi.

Any suggestions/solutions?

Thank you!
Adrian
 

adrianstanescu85

Active Member
Licensed User
Longtime User
Sure!

In Activity_Create I have:

B4X:
hc.Initialize("hc")
Dim req As HttpRequest
req.InitializeHead(MapDownloadLink)
hc.Execute(req, 1)

where MapDownloadLink is a String that holds the actual URL of interest.
Then I do:

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Log("Map file size: " & Response.ContentLength)
   CurrentMapSize = 342342342 'Response.ContentLength
   If AreNevoieDeHarta = False AND File.Exists(MapFolder, "Map.nfo") Then
          Dim sMapSize As String
        Dim OldMapSize As Long
     
        sMapSize = File.ReadString(MapFolder, "Map.nfo")
        If IsNumber(sMapSize) Then
            OldMapSize = sMapSize
        Else
            OldMapSize = 0 
        End If
        If OldMapSize <> CurrentMapSize Then
            Dim Answ, CodAnsw As Int
            CodAnsw = 0
            Answ = Msgbox2(Proceduri.MsgNewMapAvailable, Proceduri.Title1, Proceduri.ChooseYes, "", Proceduri.ChooseNo, Null)
            If Answ = DialogResponse.NEGATIVE Then
                CodAnsw = 1
                Return True
            Else
                CodAnsw = 2
            End If
            If CodAnsw = 2 Then
                'Utilizatorul doreste sa descarce noua versiune
                DescarcaHarta
            End If
        End If
   End If
   Response.Release
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
  Log("Map info error: " & StatusCode)
  Response.Release
End Sub

If I set a certain size to CurrentMapSize, like:

B4X:
CurrentMapSize = 342342342 'Response.ContentLength

then the NetworkOnMainThreadException that is visible only on mobile data plan dissappears. If I keep the code as it is (above), I get that error when using the mobile data plan (and not over Wifi).

CurrentMapSize is Long as it is declared in Process_Globals:

B4X:
Dim CurrentMapSize As Long

and the error doesn't appear when logging the moment (which includes Response.ContentLength), but apparently only when that Long variable gets the Response.ContentLength value.
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
I am using a modified version of HttpUtils2 in order to download the file that I'm interested in. The actual code is the one that is shown in one of the samples here on the site. I also have the HTTP lib ver. 1.36 active, but if I disable that then I cannot declare any HttpClient. If I add the HttpUtils2 lib then I get "Error description: HttpUtils2Service is declared twice. You should either remove the library reference or the code module.". What's there to be done?

Thank you!
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
I only have one module, slightly modified. Still the NetworkOnMainThreadException remains, what is there to be done?
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
When removing the HTTP lib (v. 1.36) and leaving only the modified HttpUtils2 module, I cannot declare any HttpClient variable.

B4X:
Dim hc As HttpClient

The hc and HttpClient appear in red and I cannot compile.
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
I don't understand how to do this. I have the HTTP lib checked, the HttpUtils2 lib unchecked, and I also have a module named HttpUtils2Service that's slightly modified. What should I do in order to be able to use HttpClient and activate the HttpUtils2?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…