A connection to XXXX was leaked.への対応


概要

JavaでAndroidの開発中、Http(s)URLConnection使用時に発生した下記エラーへの対処方法

A connection to XXXX was leaked. Did you forget to close a response body?

解決策

あまり大きな話ではなく、getInputStream()側とgetErrorStream()側でそれぞれcloseが必要みたい。

HttpsURLConnection URLconnection = null;

<>

if (URLconnection.getErrorStream() != null){
 try {
  URLconnection.getErrorStream().close();
 }catch (IOException e){
  e.printStackTrace();
 }
}

参考URL