Apache Commons - NNTP - “文章列表” - AWT

时间:2013-06-28 20:37:31

标签: java list awt apache-commons nntp

我目前正在使用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的显示屏上。任何人都可以向我解释如何将这些文章变成一个列表吗?

1 个答案:

答案 0 :(得分:0)

欢迎来到DIY新闻阅读器俱乐部。我不确定您是否想要在服务器或文章上获取新闻组列表。您已经在Iterable Collection中拥有了您的文章。通过它在每篇文章的列表中添加您想要的内容。您可能不希望在列表视图中显示整个文章正文。更有可能是消息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编码并希望以正确的顺序显示带有合适缩进和标记的线程消息列表时;)。事实上,你可以通过查看开源的土拨鼠新闻阅读器项目(我非常感激)来学习很多东西。

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