0

我正在尝试用 Java 实现比萨状态显示。因此,Java 文件正在读取 html 文件中的值并打印它。可以通过 php 脚本更改状态: http: //easternmotormall.com/teamdroidco/server/statusUpdater.php

所以脚本将状态写入文件: http ://easternmotormall.com/teamdroidco/server/home.html

Java 文件打印它。但我的主要问题是它只需要显示一次结果,我不确定如何实现它。

这是Java代码:

                url = new URL("http://easternmotormall.com/teamdroidco/server/home.html");


               try {
                   System.out.println("Fetching status from the server, please wait..");
                   String temp = null;
                   while(true){
                     URLConnection con = url.openConnection();
                     BufferedReader dataPtr = new BufferedReader(new InputStreamReader(con.getInputStream()));
                     // Printing status from Status Updater
                     String status = dataPtr.readLine();
                     if (status != null) {
                         System.out.println(status);
                    }

                     dataPtr.close();
                     Scanner in2 = new Scanner(System.in);
                     in2.nextLine();
                   }
               }
               catch (IOException e) {
                 System.err.println();
               }


        }
    catch (MalformedURLException  e)
        {
            System.err.println();
        }

我在最后暂停了循环,所以它不会继续循环。

我怎样才能使它仅在为文件提供新状态时才打印?

PS。如果有帮助的话,这最终将转换为一个 android 应用程序。

编辑:::::::::::::::::::::::::::::::::::::::::::: 这是编辑的带线程的版本:

            url = new URL("http://easternmotormall.com/teamdroidco/server/home.html");


               try {
                   System.out.println("Fetching status from the server, please wait..");
                   String temp = null;
                   while(true){
                     URLConnection con = url.openConnection();
                     BufferedReader dataPtr = new BufferedReader(new InputStreamReader(con.getInputStream()));
                     // Printing status from Status Updater
                     String status = dataPtr.readLine();
                     if (status != null) {
                         System.out.println(status);
                    }

                     dataPtr.close();
                     Scanner in2 = new Scanner(System.in);
                     in2.nextLine();
                   }
               }
               catch (IOException e) {
                 System.err.println();
               }
               statusThread statusreq = new statusThread();
               statusreq.start();
               System.out.println("After the thread...");

        }
    catch (MalformedURLException  e)
        {
            System.err.println();
        }


}




    class statusThread extends Thread {

   // 2. This is the run() method with some sample contents
   public void run() {

       String status ="", lastStatus ="";
       URL url;
       try {
       url = new URL("http://easternmotormall.com/teamdroidco/server/home.html");
           System.out.println("Fetching status from the server, please wait.. (Thread started)");
           while(true){
             URLConnection con = url.openConnection();
             BufferedReader dataPtr = new BufferedReader(new InputStreamReader(con.getInputStream()));
             // Printing status from Status Updater
             status = dataPtr.readLine();
            if (status != null && !status.equals(lastStatus)) {
                //new status!
                lastStatus = status;
                System.out.println(status);
            }
            else {
                // do nothing or log something
            }

             dataPtr.close();
             Scanner in2 = new Scanner(System.in);
             in2.nextLine();
           }
       }
       catch (IOException e) {
         System.err.println();
       }


    }
}
4

3 回答 3

0

AsyncTask如果状态已更改,我会创建一个线程(或者如果您可以使用它的 Android )以每 30 秒检查一次。如果不是 - 什么也不做,如果它改变了 - 打印它。

你可以像这样实现它:

static String lastStatus = "";

// ...

// In separate Thread or AsyncTask:

if (status != null && !status.equals(lastStatus)) {
    //new status!
    lastStatus = status;
    System.out.println(status);
}
else {
    // do nothing or log something
}
于 2012-11-23T08:16:05.280 回答
0

如果您的服务器是经过探测实现的,那么这就是 HTTP 状态的用途。

即,如果您If-Modified-Since header在请求中使用最后一次请求的日期,您将收到带有新内容的 200 ok 或未修改且没有新内容的 304。

最好, 的值If-Modified-SinceLast-Modified对您最后一个请求的响应中的标头的值。

这是做你所寻求的最标准的方式。

编辑

至于等待部分,有4个选项:

  • 在定义的时间间隔内使用定期轮询。
  • 使用长轮询系统(您的服务器尽可能长时间地阻塞连接,直到返回答案)
  • 使用通知系统(Google C2DM 或基于自定义 XMPP 的实现)
  • 使用始终打开的双向套接字

第一个选项不是很优化,但很容易。第二个选项是不久前的标准,现在仍在使用。有解决方案,比如彗星。第三种选择在架构上有所不同,但非常有效。我不推荐第四个选项,因为那里有很多事情要处理。

于 2012-11-23T08:33:29.287 回答
0

您可以在java中使用线程等待,线程的一种方法是睡眠,您可以通过它等待特定的时间......

于 2012-11-23T09:12:42.803 回答