[英]Why is my loop not printing the first line of the loop after the first iteration?
[英]java loop not printing inputstream data after first iteration
我有一個Java程序,該程序連接到在線資源,讀取數據,然后解析為一條特定的信息(這是查看特定頁面的活動Reddit帳戶的數量)。
我想自動執行此過程,以給定的間隔重復執行此過程(我將間隔設置為5秒,以查看其是否正常工作)。 然后,程序將此編號打印到文件中,每次都打印在不同的行上。 我知道主循環正在迭代,因為我的output.txt
文件有幾行,但是它只在第一次迭代中找到並打印數字。
package redditreader3;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RedditReader3 {
public static void main(String[] args) throws IOException, InterruptedException
{
int i = 1;
String host = args[0]; // www.reddit.com
String resource = args[1]; // /r/toronto/about.json
final int HTTP_PORT = 80;
String command = "GET " + resource + " HTTP/1.1\n" + "Host:" + host
+ "\n\n";
/* This command requests reddit for the source code of the resource in args[1] at its host, args[0] to be printed through HTTP. */
Socket socket = new Socket(host, HTTP_PORT);
InputStream instream = socket.getInputStream();
Scanner in = new Scanner(instream);
OutputStream outstream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
File file = new File("output.txt");
FileOutputStream F_outstream = new FileOutputStream(file);
PrintStream F_printstream = new PrintStream(F_outstream);
/* Now that the connection has been established and all of the objects
are connected to each other, the command may be sent and the data
transfer may begin. */
String ActiveAccountsData = ("\"accounts_active\": (\\d+)");
String ActiveAccountsDataFOUND;
Pattern ActiveAccountsPattern = Pattern.compile(ActiveAccountsData);
Matcher ActiveAccountsMatcher;
String input;
while(i <= 4)
{
out.print(command);
out.flush();
while(in.hasNextLine())
{
input = in.nextLine();
ActiveAccountsMatcher = ActiveAccountsPattern.matcher(input);
if(ActiveAccountsMatcher.find())
{
ActiveAccountsDataFOUND = ActiveAccountsMatcher.group(1);
F_printstream.println(ActiveAccountsDataFOUND);
break;
}
}
i++;
F_printstream.println();
Thread.sleep(5000);
}
}
}
我在想也許in.hasNextLine()
值卡在某個地方,需要更新,但是我找不到能將其返回到網站輸入開頭的方法。
您需要圍繞整個方法進行循環。 對於每次迭代,您都需要重新建立連接並解析流。
請注意,最好使用HttpURLConnection而不是使用直接套接字調用(這樣,您就不必自己處理http標頭,等等)。
您應該發出一個新的HTTP GET請求,以每5秒獲取一次更新的數據。 即在循環內移動您的InputStream / Scanner代碼。
此外,您可能想使用HttpClient而不是操作原始套接字。
從當前位置刪除以下幾行
Socket socket = new Socket(host, HTTP_PORT);
InputStream instream = socket.getInputStream();
Scanner in = new Scanner(instream);
OutputStream outstream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
將代碼更改為以下代碼,但不要忘記關閉資源(輸入流和輸出流):
while(i <= 4)
{
Socket socket = new Socket(host, HTTP_PORT);
InputStream instream = socket.getInputStream();
Scanner in = new Scanner(instream);
OutputStream outstream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
out.print(command);
out.flush();
while(in.hasNextLine())
{
input = in.nextLine();
ActiveAccountsMatcher = ActiveAccountsPattern.matcher(input);
if(ActiveAccountsMatcher.find())
{
ActiveAccountsDataFOUND = ActiveAccountsMatcher.group(1);
F_printstream.println(ActiveAccountsDataFOUND);
break;
}
}
i++;
F_printstream.println();
//close resources
Thread.sleep(5000);
}
您的掃描儀運行正常。 它正在流中並打印內容。 您要做的是倒帶視頻流。 您可以在Inputstream上使用reset來完成此操作(有關詳細信息,請參見javadoc)。 您可能需要創建一個新的掃描儀
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.