如何连续获取java应用程序的http响应?

时间:2012-10-21 04:43:14

标签: java php netbeans-7 httpresponse scheduler

我有下面显示的编码,我已经从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");
}

这个编码只返回一个响应,但我想让它连续得到响应,所以我可以根据返回的值做不同的任务。我该怎么做? 提前谢谢你:)

3 个答案:

答案 0 :(得分:0)

Sheduler.stopPHPDataChecker();
run方法中的

使得canStop = true和

if (canStop) {
    scheduler.shutdown();

}

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

答案 1 :(得分: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.

答案 2 :(得分:0)

要收到答案,您必须发布请求...

每一次!

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