2013-02-13 45 views
0

我有一個Java程序,連接到一個在線資源,讀取數據,然後解析一個特定的信息(這是查看某個頁面的活躍reddit帳戶的數量) 。java循環沒有打印輸入流數據第一次迭代後

我想自動執行此過程以給定的時間間隔重複它(爲了查看它是否工作,我將間隔設置爲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()值被卡住的地方和需要更新,但我不能找到將它返回到該網站輸入的開頭方法。

+0

你必須調試您的程序以更好地定位問題的根源。將不必要的細節暴露給我們是不好的。其次,一旦你調試你的代碼,問題就會很明顯。 – Val 2013-02-13 16:10:45

回答

-1

您的掃描儀正在按預期工作。它正在通過流並打印內容。 你想要做的是倒回流。您可以使用Inputstream上的重置來完成此操作(有關詳細信息,請參閱javadoc)。您可能需要創建一個新的掃描器

+0

'reset()'不會在套接字流上工作。 – jtahlborn 2013-02-13 16:07:55

+0

如果我倒回流,它是否仍然將最新請求的數據輸入到網站?另外,我可以在循環中聲明一個新的掃描器嗎?我認爲這是行不通的,因爲第二個聲明與第一個聲明的名稱相同。 – 1saac 2013-02-13 16:09:15

0

您需要將您的循環圍繞整個方法。對於每次迭代,您需要重新建立連接並解析流。

請注意,你最好使用HttpURLConnection而不是直接套接字調用(這樣你就不必親自處理http頭,等等)。

+0

感謝您的快速回復。你會鏈接到一個教程或使用HttpURLConnection的東西嗎?我不熟悉它。 – 1saac 2013-02-13 16:13:21

+0

@ 1saac - 我敢打賭google有_tons_指向使用HttpURLConnection的教程鏈接。 – jtahlborn 2013-02-13 16:15:20

0

您應該每5秒發出一個新的HTTP GET請求以獲取更新的數據。即在循環內移動您的InputStream/Scanner代碼。

此外,您可能需要使用HttpClient而不是原始套接字。

0

從當前位置的代碼刪除下面的線條和

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); 

更改爲以下,但不要忘了關閉資源(inputstreams和輸出流):

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); 

}

相關問題