0

我有下面显示的编码,我已经计划从 php Web 服务器页面获取 http 响应。

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

public static void main (String args[]) {
    // http request to the php page and get the response
    PHPDataChecker pdc = new PHPDataChecker();
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    final ScheduledFuture<?> pdcHandle = scheduler.scheduleAtFixedRate(pdc, 0L, 10L, TimeUnit.MILLISECONDS);// Start schedule
    scheduler.schedule(new Runnable() {

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

    }, 1L, TimeUnit.MILLISECONDS);

   do {
        if (canStop) {
            scheduler.shutdown();

        }
    } while (!canStop);

    System.out.println("END");
}

此编码仅返回一个响应,但我希望它连续获得响应,以便我可以根据返回的值执行不同的任务。我该怎么做?先感谢您 :)

4

3 回答 3

0

-我的简单建议是使用 Thread,它会在一定的时间间隔后向 web 服务发送请求,并接收响应。

    Thread t = new Thread(new Runnable(){

          public void run(){

   while(isDone){  

             //isDone is the boolean variable which will help u end the Thread.

            // SEND THE REQUEST 

try{

  Thread.sleep(5000);   // Delay of 5 seconds before the request is fired again

   }catch(Exception ex){

           ex.printStackTrace();

        }
    }
   }

    });

 t.start();

 t.join();   // Use join() to block the process further till the response arrives.
于 2012-10-21T05:11:19.617 回答
0

要获得答案,您必须发布请求...

每一次 !

将请求帖子放入您的定期循环中。

于 2012-10-21T07:57:17.693 回答
0
Sheduler.stopPHPDataChecker();

在 run 方法中使 canStop= true ,并且

if (canStop) {
    scheduler.shutdown();

}

使调度程序关闭。从 run 方法中删除 stopPHP.. 方法将解决此问题。您可能已重新考虑代码。看起来令人困惑。

于 2012-10-21T04:53:55.977 回答