简体   繁体   中英

Can java.util.zip.GZIPInputStream.close cause android.os.NetworkOnMainThreadException

The App works fine on the 2.2.1 and 2.2 but is causing the following error on 4.0.3

android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
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.ChunkedInputStream.read(ChunkedInputStream.java:161)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:175)
at org.apache.http.impl.io.ChunkedInputStream.exhaustInputStream(ChunkedInputStream.java:289)
at org.apache.http.impl.io.ChunkedInputStream.close(ChunkedInputStream.java:262)
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.FilterInputStream.close(FilterInputStream.java:64)
at java.util.zip.InflaterInputStream.close(InflaterInputStream.java:256)
at java.util.zip.GZIPInputStream.close(GZIPInputStream.java:148)
at com.myapp.apper.contact.ContactService.a(SourceFile:390)
at com.myapp.apper.contact.ContactService.a(SourceFile:347)
at com.myapp.apper.contact.ContactService.a(SourceFile:41)
at com.myapp.apper.contact.g.a(SourceFile:322)
at com.myapp.apper.contact.q.a(SourceFile:58)
at com.myapp.apper.contact.q.onPostExecute(SourceFile:23)
at android.os.AsyncTask.finish(AsyncTask.java:602)
at android.os.AsyncTask.access$600(AsyncTask.java:156)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4974)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

All my network operations are in AsyncTask and from the stacktrace looks like the errors is caused by java.util.zip.GZIPInputStream.close and it also points towards the like 390 of the code which is gzipIn.close(); .

Is GZIPInputStream causint this error? If not, what could be the cause of the error?

It looks like you are reading something using GZIPInputStream in the onPostExecute() of async task which runs in UI thread. From 4.0 onward it is not allowed to have network connection in UI thread. I would suggest to move the GZIPInputStream reading part in the doInBackground() method.

Edit1: or you can disable the strict mode by following code:

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM