0

以下に示すコーディングを使用して、PHPを使用してサーバーのSQLデータベースからデータを取得できました。データベースを定期的にチェックして、新しいデータが追加されたかどうかを確認する必要があります。追加された場合は、それらを Java アプリケーションに取得する必要があります。私は netbeans IDE を使用して いますが、どうすればこれを行うことができますか?

try {
    URL url = new URL("http://taxi.com/login.php?param=10");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

  } catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}
4

1 に答える 1

0

このソリューションでは、少なくともJava 5を使用していると想定しています。
5 分ごとに新しいデータをチェックし、25 分後に終了するとします。
あなたはこれをします:

PHPDataChecker.java

public class PHPDataChecker implements Runnable {
    public void run() {
       // Paste here all the code in your question
    }
}

Main.java

public class Main {
    private static boolean canStop=false;

    private static void stopPHPDataChecker() {
        canStop=true;
    }

    public static void main(String[] args) {
        // Setup a task for checking data and then schedule it
        PHPDataChecker pdc = new PHPDataChecker();
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        final ScheduledFuture<?> pdcHandle = scheduler.scheduleAtFixedRate(pdc, 0L, 5L, TimeUnit.MINUTES);// Start pooling

        // Setup a new task to kill the polling after 25 minutes
        scheduler.schedule(new Runnable() {

            public void run() {
                System.out.println(">> TRY TO STOP!!!");
                pdcHandle.cancel(true);
                Main.stopPHPDataChecker();
                System.out.println("DONE");
            }

        }, 25L, TimeUnit.MINUTES);

        // Actively wait stop condition (canStop)
        do {
            if (canStop) {
                scheduler.shutdown();
            }
        } while (!canStop);

        System.out.println("END");
    }
}
于 2012-09-18T08:12:50.877 に答える