title: Repeated invocations of Twitter Search API getting stalled after a couple of times
body: <p>I am using Twitter Search API to get count of tweets on a given search string. Since API returns only number of tweets specified by 'count' parameter in the URL, I have to send REST call to the URL multiple  times.</p>
  
  <p>I parse the JSON response returned after first call and get the ID of the last tweet and send that as 'max_id' in the URL next time to get more tweets.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>private static void sendSearchRequest(String token, String maxId) throws Exception {
      System.out.println("Entering sendSearchRequest");
      byte[] temp = Base64.encodeBase64(token.getBytes());
      String url = "https://api.twitter.com/1.1/search/tweets.json?q=%23SearchString&amp;count=100&amp;include_entities=false";
  
      //Will be null in first invocation, not null in subsequent calls
      if(maxId != null)
          url += "&amp;max_id="+maxId;
  
      URL obj = new URL(url);
      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
      con.setRequestProperty("Authorization", "Bearer " + token);
  
      System.out.println("\nSending 'GET' request to URL : " + url);
      // optional default is GET
      con.setRequestMethod("GET");
      int responseCode = con.getResponseCode();
  
      System.out.println("Response Code : " + responseCode);
  
      BufferedReader in = new BufferedReader(
              new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
      con.disconnect();
  
      //Resend the request till response is not blank.
      if(response.toString().length() &gt; 0){
          maxId = parseResponse(response.toString());
          System.out.println(positiveCount);
          sendSearchRequest(token, maxId);
      }else{
          System.out.println("No more results");
          return;
      }
  
      System.out.println("Exiting parseResponse");
  }
  
  private static String parseResponse(String response) {
      System.out.println("Entering parseResponse");
      String lastId = "";
      JSONParser parser=new JSONParser();
      try{
          Object object = parser.parse(response);
          JSONObject array = (JSONObject)object;
          JSONArray jsonArray = (JSONArray)array.get("statuses");
          int size = jsonArray.size();
          positiveCount += size;
          System.out.println(size);
  
          //Find the index of last object in the array returned by API
          JSONObject lastObject = (JSONObject)jsonArray.get(size-1);             
  
          //Get Tweet ID to be set as Max_id in next call to Twitter.
          Long id = (Long)lastObject.get("id");
          System.out.println(id);
          lastId = id+"";
      }catch(Exception e){
          e.printStackTrace();
      }
      System.out.println("Exiting parseResponse");
      return lastId;
  
  }
  </code></pre>
  
  <p>The problem I am facing is that this code makes calls only couple of times. After that the call is not sent and program just hangs. I suspected it could be due to stale open connection, so I closed that manually but even that is not working.</p>
  
  <p>Can anyone please tell me what is wrong here?</p>
  
  <p>Thanks
  Abhishek</p>