导言:
一位女同事开发过程中用了MVP框架开发,当在上报文件接口时出现这个问题
异常信息:
021-04-25 15:24:38.118 2369-2369/com.wavenet.yundp W/System.err: android.os.NetworkOnMainThreadException
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:175)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:144)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at okio.InputStreamSource.read(Okio.kt:102)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:159)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:51)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at okhttp3.internal.connection.RealConnection.isHealthy(RealConnection.kt:626)
2021-04-25 15:24:38.121 2369-2369/com.wavenet.yundp W/System.err: at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:126)
解决:
由于上报图片文件会比较耗时,故必须在子线程请求上传,给她看了一下,是漏了compose这段代码,她只是疏忽大意
Api.getDefault(HostType.BASE_URL).uploadFile(parts).compose(RxSchedulers.<ResponseBody>io_main());