У меня случайная и странная проблема с Android HttpsURLConnection. When HttpsURLConnection.getResponseCode (); называется, java .io.InterruptedIOException выбрасывается. Все сетевые операции выполняются внутри AsyncTask. Это происходит на производстве, и в настоящее время оно затрагивает более 2 тысяч пользователей в день на множестве различных устройств и версий ОС. Нет проблем с подключением inte rnet, также не срабатывает тайм-аут. Есть идеи о том, что искать? Вот мой код.
URL url;
String response = "";
int resCode=200;
try {
url = new URL(requestURL);
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return Servers.Companion.verifyHostName(hostname);
}
};
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(hostnameVerifier);
conn.setSSLSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory());
conn.setReadTimeout(120000);
conn.setConnectTimeout(30000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
byte[] outputBytes = json.getBytes("UTF-8");
conn.connect();
OutputStream os = conn.getOutputStream();
os.write(outputBytes);
resCode = conn.getResponseCode();//this triggers the exception
long size = conn.getContentLength();
int total = 0;
int count = 0;
InputStream in = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream w = new DataOutputStream(baos);
byte data[] = new byte[4096];
while ((count = in.read(data)) != -1) {
if (isCancelled()) {
in.close();
return new ResponseOb(-100,"Downloading Canceled");
}
if(count>0) {
w.write(data, 0, count);
w.flush();
}
total += count;
if (size > 0) {
publishProgress((int) (total * 100 / size));
}
}
.....
и вот исключение:
java.io.InterruptedIOException: thread interrupted
at com.android.okhttp.okio.Timeout.throwIfReached(Timeout.java:145)
at com.android.okhttp.okio.Okio$1.write(Okio.java:73)
at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:902)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
at helectronsoft.com.live.wallpaper.pixel4d.network.ListDownloader.performPostCall(ListDownloader.java:317)
at helectronsoft.com.live.wallpaper.pixel4d.network.ListDownloader.doInBackground(ListDownloader.java:164)
at helectronsoft.com.live.wallpaper.pixel4d.network.ListDownloader.doInBackground(ListDownloader.java:46)
at android.os.AsyncTask$2.call(AsyncTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:246)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:833)