title: Java HTTP Response Body leaked... but never explicitly using a response body?
body: <p>I am working on an app in Android Studio that reads information from a REST API, parses it client-side, and then displays it. Everything is working and nothing crashes so far, but I keep getting the following log warning: 
  <code>W/OkHttpClient: A connection to https://&lt;site&gt;.com/ was leaked. Did you forget to close a response body?</code> I have a <code>cleanup()</code> function that closes my <code>InputStream</code> and <code>JsonReader</code>, and as you can see, I make sure to disconnect my HTTPS connection. This is the only place I use any type of networking logic. What else do I need to close? </p>
  
  <pre><code> protected static JsonReader getAPI(String endpointURL) {
      JsonReader jsonReader = null;
      try {
          URL url = new URL(endpointURL);
          // Create connection
          myConnection = (HttpsURLConnection) url.openConnection();
          if (myConnection.getResponseCode() == 200) {
              // Success
              // Further processing here
              InputStream inp = myConnection.getInputStream();
              try {
                  jsonReader = new JsonReader(new InputStreamReader(inp, "UTF-8"));
                  jsonReader.setLenient(true);
                  //inp.close();
              } catch (Exception e) {
                  Log.i("Serverside", inp.toString());
              }
          } else {
              // Error handling code goes here
              //hits here if no events! this is okay
              Log.i("Serverside", "URLConnection");
          }
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (Exception e) {
          System.out.println("Error " + e.getMessage());
      }
      finally {
          myConnection.disconnect();
      }
      return jsonReader;
  }
  </code></pre>
  
  <p>EDIT: Here is a snippet of the logs:</p>
  
  <pre><code>03-31 06:59:16.758 15809-15809/com.rsenapp.triage I/Fix error: back pressed
  03-31 06:59:16.785 15809-15924/com.rsenapp.triage V/FA: Recording user engagement, ms: 8279
  03-31 06:59:16.785 15809-15924/com.rsenapp.triage V/FA: Connecting to remote service
  03-31 06:59:16.798 1596-1609/? E/memtrack: Couldn't load memtrack module
  03-31 06:59:16.799 1596-1609/? W/android.os.Debug: failed to get memory consumption info: -1
  03-31 06:59:16.811 15809-15924/com.rsenapp.triage V/FA: Activity paused, time: 15659169
  03-31 06:59:16.822 15809-15924/com.rsenapp.triage D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=8279, firebase_screen_class(_sc)=EventviewActivity, firebase_screen_id(_si)=-579304082464371239}]
  03-31 06:59:16.870 15809-15924/com.rsenapp.triage V/FA: Connection attempt already in progress
  03-31 06:59:19.396 15809-15934/com.rsenapp.triage I/Serverside: URLConnection
  03-31 06:59:19.402 15809-15934/com.rsenapp.triage I/System.out: [Ljava.lang.StackTraceElement;@34c53
  03-31 06:59:20.579 15809-15934/com.rsenapp.triage I/Serverside: URLConnection
  03-31 06:59:20.581 15809-15934/com.rsenapp.triage I/System.out: [Ljava.lang.StackTraceElement;@ddaf990
  03-31 06:59:21.571 15809-15934/com.rsenapp.triage I/Serverside: URLConnection
  03-31 06:59:21.573 15809-15934/com.rsenapp.triage I/System.out: [Ljava.lang.StackTraceElement;@3a65289
  03-31 06:59:21.792 1390-1528/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 6962632 , only wrote 6714000
  03-31 06:59:23.019 15809-15884/com.rsenapp.triage W/OkHttpClient: A connection to https://rsenapp.com/ was leaked. Did you forget to close a response body?
  03-31 06:59:23.020 15809-15884/com.rsenapp.triage I/chatty: uid=10082(com.rsenapp.triage) OkHttp Connecti identical 9 lines
  03-31 06:59:23.020 15809-15884/com.rsenapp.triage W/OkHttpClient: A connection to https://rsenapp.com/ was leaked. Did you forget to close a response body?
  03-31 06:59:23.589 1596-1658/? I/InputDispatcher: Application is not responding: AppWindowToken{c65626d token=Token{8968a84 ActivityRecord{2677897 u0 com.rsenapp.triage/.MainActivity t49}}}.  It has been 5006.8ms since event, 5003.5ms since wait started.  Reason: Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.
  03-31 06:59:23.590 1596-1658/? I/WindowManager: Input event dispatching timed out sending to application AppWindowToken{c65626d token=Token{8968a84 ActivityRecord{2677897 u0 com.rsenapp.triage/.MainActivity t49}}}.  Reason: Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.
  03-31 06:59:23.620 1596-1658/? W/InputDispatcher: Asynchronous input event injection timed out.
  03-31 06:59:23.724 15809-15809/com.rsenapp.triage I/Serverside: [com.rsenapp.triage.Event@9ed128e, com.rsenapp.triage.Event@d6c5aaf, com.rsenapp.triage.Event@d1e1ebc, com.rsenapp.triage.Event@4988e45, com.rsenapp.triage.Event@eabc99a, com.rsenapp.triage.Event@dadf2cb, com.rsenapp.triage.Event@70c6aa8, com.rsenapp.triage.Event@eb8c9c1, com.rsenapp.triage.Event@e6ce566, com.rsenapp.triage.Event@526f0a7, com.rsenapp.triage.Event@7118954]
  03-31 06:59:23.724 15809-15809/com.rsenapp.triage I/EventList: Current Event: rtCatpcha: Video, voice, and smarts for secure authentication
  03-31 06:59:23.724 15809-15809/com.rsenapp.triage I/EventList: Current Event: Distributed Algorithms for Pattern Matching in Massive Labelled Graphs
  03-31 06:59:23.724 15809-15809/com.rsenapp.triage I/EventList: Current Event: Privacy, Security, Internet, and the Coming Clash of Cultures
  03-31 06:59:23.724 15809-15809/com.rsenapp.triage I/EventList: Current Event: Joshua Sorenson: Solving the Biggest Problems 
  03-31 06:59:23.724 15809-15809/com.rsenapp.triage I/EventList: Current Event: More Data, More Science and… Moore’s Law?
  </code></pre>
  
  <p>Note that the stack traces that occur around the warning message are expected -- they are getting printed because, as we loop through all of the API routes, we sometimes reach lists that do not yet have information to share (e.g. a group that has no current events). Also, for right now, the data is pulled completely fresh with each change of screen -- we do not store the pulled data to pass between screens. The warning appears pretty consistently whenever a screen with API-pulled data is displayed. </p>
  
  <p>ANOTHER EDIT: Here are the class fields and how they are closed via a cleanup method:</p>
  
  <pre><code>public class JSONParser {
  
      private static HttpsURLConnection myConnection;
      private static InputStream inp;
      private static JsonReader jsonReader;
  
      private static void cleanup() {
          if (inp != null) {
              try {
                  inp.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          if (jsonReader != null) {
              try {
                  jsonReader.close();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  </code></pre>