title: Single URLConnection to fetch only the first line in java using BufferedReader
body: <p>I am trying to write a Java program that should automatically download text from a website if and only if it gets updated. The problem I am running into is using only one HTTPURLConnection to do that because if i don't there will be billions of HTTPURLConnections to the web server since I am using a while(true) loop. Here is my work-in-progress, the getMsg() method receives a url and opens an HTTPURLConnection. Currently I am starting a new connection every time I have to read a line, which is not the most efficient way I am sure. How do I keep reading the same line with the same HTTPURLConnection?</p>
  
  <pre><code>// Starts a new URLConnection to "localhost/currentmsg.dat"
  // Receives JSON string from the URLConnection
  // Sets up a different variable for each object received from the URL for eg. if delete=1 means that the admin is requesting to delete a message from the screen.
  
  import java.io.*;
  import java.net.*;
  
  import org.json.*;
  import java.io.*;
  import java.util.Scanner;
  
  public class jListenerURL {
  
  // Current arguments retrieved from the URL
  private static int msgID = 0;
  private static int aptID = 1; // Apartment ID of current device
  private static int unitID = 3; // Unit ID of current device
  static String message; // Message received from admin
  static int delete; // Delete a message?
  static int dmsgID; // What message to delete?
  
  public static void jListener() {
  
      URL url;
      boolean keepGoing = true;
      String msg = "";
  
      try {
          url = new URL("http://www.lotussmoke.com/msgtest/currentmsg.dat");
          while (keepGoing) {
              msg = getMsg(url);
  
              JSONObject jObj = null;
  
              try {
                  jObj = new JSONObject(msg);
              }
  
              catch (JSONException je) {
                  System.out.println("JSON Exception for message, try restarting terminal.");
              }
  
              int current = jObj.getInt("msgID");
              int targetaptID = jObj.getInt("aptID");
              int targetunitID = jObj.getInt("unitID");
  
              // Keep listening, if the message changes meaning a different msgID then print that message
              if (current!=msgID &amp;&amp; targetaptID == aptID &amp;&amp; targetunitID == unitID) {
  
                  msgID = jObj.getInt("msgID");
                  message = jObj.getString("message");
                  delete = jObj.getInt("delete");
                  dmsgID = jObj.getInt("dmsgID");
  
                  if (delete==1) {
                      // Delete a message
                      System.out.println("Delete msg ID? " + dmsgID);
                      continue;
                  }
  
                  System.out.println("Message ID: " + msgID);
                  System.out.println("Apartment ID: " + aptID);
                  System.out.println("Unit ID: " + unitID);
                  System.out.println("Message: " + message);
              }
          }
      }
  
      catch (MalformedURLException e) {
          System.err.println();
      }
  }
  
    public static void main(String args[]) throws Exception {
        jListener();
        }
  
    private static String getMsg(URL url) {
          HttpURLConnection con = null;
          try {
              con = (HttpURLConnection) url.openConnection();
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          BufferedReader in = null;
          String msg = "";
          try {
              in = new BufferedReader(new InputStreamReader(con.getInputStream()));
              String received;
              while((received = in.readLine()) != null) {
                  //System.out.println(received);
                  msg = received;
                  }
              in.close();
              }
          catch (IOException e) {
              e.printStackTrace();
              }
          return msg;
    }
  </code></pre>
  
  <p>}</p>