Android Question What is "NetworkOnMainThread..."?

boten

Active Member
Licensed User
I'm trying to modify an app. The app's old version works fine on all my devices (Galaxy S with froyo and GalaxyS4 JB).
now, when I re-compile (no changes yet to code) it runs OK on the emulator, on the GalaxyS, BUT on the GalaxyS4
it crashes with:
B4X:
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


android.os.NetworkOnMainThreadException


   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
   at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
   at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
   at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
   at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
   at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
   at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
   at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
   at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
   at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
   at org.apache.http.impl.io.ContentLengthInputStream.close(ContentLengthInputStream.java:121)
   at org.apache.http.conn.BasicManagedEntity.streamClosed(BasicManagedEntity.java:179)


   at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:266)
   at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:213)
   at java.io.InputStreamReader.close(InputStreamReader.java:145)
   at org.apache.http.util.EntityUtils.toString(EntityUtils.java:139)
   at anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper.GetString(HttpClientWrapper.java:489)
   at boten.jbn2.main._hc_responsesuccess(main.java:1073)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
   at anywheresoftware.b4a.BA$3.run(BA.java:320)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5328)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
   at dalvik.system.NativeStart.main(Native Method)
android.os.NetworkOnMainThreadException

What's wrong?

"Old" version compiled with an old version of B4A
 

DonManfred

Expert
Licensed User
See this Snippet.
But useually your code is wrong (calling networkroutines on Mainthread) and you should avoid doing this. But without seeing any code from you it is hard to give an concrete advice what´s wrong.
 
Upvote 0

boten

Active Member
Licensed User
Think I found the solution, though I'd like to understand why?
old version compile manifet had:
B4X:
<uses-sdk android:minSdkVersion="4" />

new version compile manifet has:
B4X:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>

I changed the manifest and removed the targetsdk...=14 and now it works.
Why?
 
Upvote 0

boten

Active Member
Licensed User
Thanks, DonManfred! I used the DisableStrictMode code (manifest with android:targetSdkVersion="14"/>) and it works.
 
Upvote 0

boten

Active Member
Licensed User
You should not use HttpResponse.GetString. This is a deprecated method. You should instead use HttpUtils2.
Thx. In addition of being easier, it solved the problem of some HTML pages I read are in UTF8 and some are ISO-8859-8.
HttpResponse.GetString would not let me do it twice (according to charset tag in html).
with HttpJob.Getstring2 I can examine the charset=... and do HttpJob.Getstring2 with a different parameter.
 
Upvote 0

ibonk64

Member
Licensed User
how to fix it without change manifest. because if android:targetSdkVersion remove. the style app change to os 2.3. tks
 
Upvote 0

jaraiza

Active Member
Licensed User
Just to let you know, I used an old routine I did with "hostLoad", "host_ResponseSuccess" and "host_ResponseError" to read JSON. I did because I had it, it always worked, and I was in a rush.

Now suddenly it stopped working reliable (the JSON returns a list of names to be displayed in a spinner), in a weird but consistent way. If I use a debug version, everything works ok (so I can't debug the error), but in release mode if I search for "jaime" everything works ok; if I search for "flores" then the same error is thrown:
B4X:
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
I was able to workaround the error with the info posted here (manifiest change)

Since everything else I did in the app was made with httputils2 I guess I should begin to migrate my old routine, but I wanted to help adding more details to this weird error.
 
Upvote 0
Top