【问题标题】:java loop not printing inputstream data after first iteration第一次迭代后java循环不打印输入流数据
【发布时间】:2013-02-13 15:51:50
【问题描述】:

我有一个 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() 值可能卡在某个地方并且需要更新,但我找不到将其返回到网站输入开头的方法。

【问题讨论】:

  • 您必须调试您的程序才能更好地定位问题的根源。让我们接触不必要的细节是不好的。其次,一旦调试代码,问题就会很明显。

标签: java loops inputstream


【解决方案1】:

您需要围绕 整个 方法放置循环。对于每次迭代,您都需要重新建立连接并解析流。

注意,您最好使用 HttpURLConnection 而不是使用直接套接字调用(这样您就不必自己处理 http 标头等)。

【讨论】:

  • 感谢您的快速回复。您是否有指向教程或使用 HttpURLConnection 的链接?我对它不熟悉。
  • @1saac - 我敢打赌,谷歌有 的链接到使用 HttpURLConnection 的教程。
【解决方案2】:

您应该每 5 秒发出一个新的 HTTP GET 请求以获取更新的数据。即在循环内移动您的 InputStream/Scanner 代码。

此外,您可能希望使用HttpClient 而不是操作原始套接字。

【讨论】:

    【解决方案3】:

    从当前位置删除下面的行并

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

    }

    【讨论】:

      【解决方案4】:

      您的扫描仪运行正常。它正在通过流并打印内容。 你想要做的是倒带流。您可以在 Inputstream 上使用 reset 来执行此操作(有关详细信息,请参阅 javadoc)。您可能需要创建一个新的 Scanner

      【讨论】:

      • 如果我倒回流,它仍然会为网站输入最新请求的数据吗?另外,我可以在循环内声明一个新的扫描仪吗?我认为这行不通,因为第二个声明与第一个声明同名。
      猜你喜欢
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 2021-03-13
      • 1970-01-01
      相关资源
      最近更新 更多