2013-02-13 56 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); 

}

相关问题