Reputation: 680
public class ReadIp{
String nextLine;
public int getIP() throws MalformedURLException, IOException {
try
{
URL url = null;
URLConnection urlConn = null;
InputStreamReader inStream = null;
BufferedReader buff = null;
url = new URL("http://test.myrywebsite.co.uk/");
urlConn = url.openConnection();
inStream = new InputStreamReader(
urlConn.getInputStream());
buff = new BufferedReader(inStream);
while ((nextLine = buff.readLine()) != null) {
System.out.println(nextLine);
}}
catch(Exception ex)
{}
return 0;
}
public static void main(String[] args) throws MalformedURLException, IOException {
ReadIp rp = new ReadIp();
rp.getIP();
}
}
I have this code above to extract information from the specified website. I have a website where it shows a sentence. Every time I reload the webpage, it will show a different sentence.
At the moment, with the code above, I need to manually run the program to show different sentences. Is there way where the program automatically reloads by itself? Do I use TimerTask?
Upvotes: 0
Views: 133
Reputation: 365
public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
ReadIp rp = new ReadIp();
while (true)
{
rp.getIP();
//wait 4 seconds
Thread.sleep(4000);
}
}
Upvotes: 0
Reputation: 4475
public static void main(String args[]) throws Exception
{
ReadIp rp=new ReadIp();
while(true)
{
rp.getIp();
Thread.sleep(1000); // sleep 1 second
}
}
This will run forever or untill you kill the process.
Remark: also integrate inStream.close() somewhere to liberate resources.
Upvotes: 1