0

我目前正在使用 Apache Commons Net 开发我自己的 NNTP 阅读器。使用可用的教程,我能够使用他们的一些代码来让我取回文章。

我在 NNTP 部分使用的代码 -

System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);

System.out.println("Building message thread tree...");
Threader threader = new Threader();
Article root = (Article)threader.thread(articles);
Article.printThread(root, 0);

我需要获取文章并将它们转换为 List 类型,以便我可以使用类似这样的方式将它们发送到 AWT -

List x = (List) b.GetGroupList(dog);
        f.add(CreateList(x));

我本节的整个代码库是 -

public void GetThreadList(String Search) throws SocketException, IOException {

        String hostname = USE_NET_HOST;
        String newsgroup = Search;

        NNTPClient client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
        client.connect(hostname);
        client.authenticate(USER_NAME, PASS_WORD);

        if(!client.authenticate(USER_NAME, PASS_WORD)) {
            System.out.println("Authentication failed for user " + USER_NAME + "!");
            System.exit(1);
        }

        String fmt[] = client.listOverviewFmt();
        if (fmt != null) {
            System.out.println("LIST OVERVIEW.FMT:");
            for(String s : fmt) {
                System.out.println(s);
            }
        } else {
            System.out.println("Failed to get OVERVIEW.FMT");
        }
        NewsgroupInfo group = new NewsgroupInfo();
        client.selectNewsgroup(newsgroup, group);

        long lowArticleNumber = group.getFirstArticleLong();
        long highArticleNumber = lowArticleNumber + 5000;

        System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
        Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);

        System.out.println("Building message thread tree...");
        Threader threader = new Threader();
        Article root = (Article)threader.thread(articles);
        Article.printThread(root, 0);

        try {
            if (client.isConnected()) {
                client.disconnect();
                }
            }
            catch (IOException e) {
                System.err.println("Error disconnecting from server.");
                e.printStackTrace();
            }
    }

和 -

public void CreateFrame() throws SocketException, IOException {
        // Make a new program view
        Frame f = new Frame("NNTP Reader");
        // Pick my layout
        f.setLayout(new GridLayout());
        // Set the size
        f.setSize(H_SIZE, V_SIZE);
        // Make it resizable
        f.setResizable(true);
        //Create the menubar
        f.setMenuBar(CreateMenu());
        // Create the lists
        UseNetController b = new UseNetController(NEWS_SERVER_CREDS);
        String dog = "*";
        List x = (List) b.GetGroupList(dog);
        f.add(CreateList(x));

        //f.add(CreateList(y));
        // Add Listeners
        f = CreateListeners(f);
        // Show the program
        f.setVisible(true);
    }

我只想获取返回的新闻文章列表并将它们发送到 AWT 中的显示器。谁能向我解释如何将这些文章变成列表?

4

1 回答 1

0

欢迎来到 DIY 新闻阅读器俱乐部。我不确定您是否正在尝试获取服务器上的新闻组列表或文章。您已经在可迭代集合中拥有文章。遍历它,在每篇文章的列表中附加您想要的内容。您可能不想在列表视图中显示整个文章正文。更有可能是消息 ID、主题、作者或日期(或作为字符串的组合)。例如,对于仅包含主题的列表:

...
Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
Iterator<Article> it = articles.iterator();
while(it.hasNext()) {
    Article thisone = it.next();
    MyList.add(thisone.getSubject()); 
   //MyList should have been declared up there somewhere ^^^ and  
   //your GetThreadList method must include List in the declaration
}
return MyList;
...

我的策略是通过迭代器将文章检索到 SQLite 数据库中,其中正文、主题、引用等存储在字段中。然后,您可以创建一个按您想要的方式排序的列表,并通过主键链接来检索您在显示单个文章时需要的内容。另一种策略是一组 message_ids 或文章编号,并根据需要从新闻服务器中单独获取每一个。玩得开心 - 特别是当您为 Android 编码并希望以正确的顺序显示带有合适缩进和标记的线程消息列表时;)。事实上,通过查看开源 Groundhog 新闻阅读器项目(我永远感激不尽),您可以学到很多东西。

http://bazaar.launchpad.net/~juanjux/groundhog/trunk/files/head:/GroundhogReader/src/com/almarsoft/GroundhogReader

于 2014-11-28T16:05:32.053 回答