0

I am currently using Apache Commons Net to develop my own NNTP reader. Using the tutorial available I was able to use some of their code to allow me to get articles back.

The Code I am using from NNTP Section -

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

I need to take the articles and turn them into a List type so I can send them to AWT using something like this -

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

My Entire code Base for this section is -

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

and -

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

I just want to take my list of returned news articles and send them to the display in AWT. Can any one explain to me how to turn those Articles into a list?

ILikeTurtles
  • 1,012
  • 4
  • 16
  • 47
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. 3) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jun 29 '13 at 06:11
  • 1. Ok. 2. The question is focused on "Article" which is from the Apache Commons Library. The choice of x or dog for my own variables is for time purposes. They get changed later. "Article" comes from Apache - if we can fix my question I will be willing to send them an email to get them to change their variable names if that is the problem. 3. http://www.pitman.co.za/projects/charva/index.html - I'm not looking to abandon AWT for Swing. I am looking to use AWT so I can use charva. – ILikeTurtles Jul 01 '13 at 15:41
  • The other issue at hand; you need a news server account to use the program. So I can upload it; but only people with access to their own news server could then run the program. – ILikeTurtles Jul 01 '13 at 15:45

1 Answers1

0

Welcome to the DIY newsreader club. I'm not sure if you are trying to get a list of newsgroups on the server, or articles.You have already have your Articles in an Iterable Collection. Iterate through it appending what you want in the list from each article. You probably aren't going to want to display the whole article body in a list view. More likely the message id, subject, author or date (or combination as a string). For example for a List of just subjects:

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

My strategy has been to retrieve the articles via an iterator in to an SQLite database with the body, subject, references etc. stored in fields. Then you can create a list sorted just how you want, with a link by primary key to retrieve what you need for individual articles as you display them. Another strategy would be an array of message_ids or article numbers and fetch each one individually from the news server as required. Have fun - particularly when you are coding for Android and want to display a list of threaded messages in the correct sequence with suitable indents and markers ;). In fact, you can learn a lot by looking at the open source Groundhog newsreader project (to which I am eternally grateful).

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

PSF
  • 1