NetworkOnMainThreadException error

rickrack

Member
Licensed User
Longtime User
Hello, sometimes I get the NetworkOnMainThreadException when I run my app... I'm using a Galaxy Nexus with Jelly Bean. The app uses HttpUtils to download data from an online .php page.
I read on the forum that I should use AsyncStreams, but I didn't understand the difference between this and AsyncStreams... Can you explain it?
Thank you in advance.
 
AsyncStreams is not related.

The problem is in this code:
Code: Copy
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   countWorking = countWorking - 1
   finishTasks = finishTasks + 1
   HandleError(TaskId, Reason)
   If Response <> Null Then
      Log(Response.GetString("UTF8"))
      Response.Release
   End If
   ProcessNextTask
End Sub
You can add a Try Catch block to ignore errors related to Response.GetString.

Other options:
- Use HttpUtils2 instead of HttpUtils
- Wait for B4A v2.30. The Http library was updated and this error will not happen.
 
Upvote 0
Thanks for your help Erel, but I didn't understand.. you are showing me a part of code that regards the ResponseError case but the debug says that it happens at "ResponseSuccess"..
The error happens on this line:
Code: Copy
Sub hc_info_ResponseSuccess(Response As HttpResponse, TaskId As Int)

   ProgressDialogHide
   statoconnessione = "connesso"

   vet_info = Regex.Split(CRLF, Response.GetString("UTF8")) '<- this is the line

sometimes it works.. but in most cases it gives the error...
 
Upvote 0
Oh... I'm sorry, I didn't pay attention to what I was doing. I'll try with HttpUtils2.
Thank you, have a nice day

*EDIT*
HttpUtils2 works very good :)
 
Last edited:
Upvote 0
Back
Top