繁体   English   中英

如何连续获得对Java应用程序的HTTP响应?

[英]how to get http responses continuously to a java application?

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

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

Sheduler.stopPHPDataChecker();

在run方法内部,可以使canStop = true和

if (canStop) {
    scheduler.shutdown();

}

使调度程序关闭。从run方法中删除stopPHP ..方法将解决此问题。您可能已经重构了代码。令人困惑。

-我的简单建议是使用线程,线程将在固定的时间间隔后向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.

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

每一次 !

将请求发布放入您的定期循环中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM