Cannot access request header fields after connection is set 解决办法

今天测试时,发现有个地方没修改过代码,但是却出现一个奔溃问题。

java.lang.IllegalStateException: Cannot access request header fields after connection is set
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getRequestProperties(HttpURLConnectionImpl.java:231)
    at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getRequestProperties(TrackedHttpURLConnection.java:204)
    at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.trackPreConnect(TrackedHttpURLConnection.java:63)
    at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getInputStream(TrackedHttpURLConnection.java:354)
    at com.android.tools.profiler.support.network.httpurl.HttpURLConnection$.getInputStream(HttpURLConnection$.java:207)

通过多次尝试,发现网上有个方法可以解决:
inputStream = connection.getInputStream();放到int fileLength = connection.getContentLength();前面即可。
之前一直没问题,但是升级了buildToolsVersioncompileSdkVersion版本就出现此问题了。

    原文作者:chenxuxu
    原文地址: https://www.jianshu.com/p/a1f82f0f86b5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞