Implementing the invocation classes

TextViewer class

The TextViewer class displays the invoked text content in a Form. It establishes a HTTP connection with the invocation URL to get the content. In this example, the URL for the text content is located in the res/links.txt file that is defined in the JAD file

  1. Create the TextViewer class file.

  2. Import the required classes and assign this class to the com.nokia.midp.example.jsr211.texthandler package.

    package com.nokia.midp.example.jsr211.texthandler;
    
    import java.io.InputStream;
    
    import javax.microedition.content.Invocation;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.StringItem;
    
  3. Create the main class and the objects to be used in this application:

    public class TextViewer extends Form implements CommandListener{
    
    	/** Intial title of the text display form*/
    	private static final String DEFAULT_TITLE = "Text Viewer";
    	
    	/** Message saying.. No content to display*/
    	private static final String NO_CONTENT = "";
    
    	/** Instance of TextHandler used to display form*/
    	private TextHandler textHandler = null;
    	
    	/** Instance of invocation. Get value from the constructor*/
    	private Invocation invoc = null;
    	
    	Command detailCommand = new Command("Details",Command.OK,1);
    	
    	/** Go back to the choice list window*/
    	Command backCommand = new Command("Back", Command.OK, 2);
    

    Create the constructor for TextViewer instances.

    	/**
    	 * @param _invoc Text Invocation instance.
    	 * @param _textHandler Instance of TextHandler
    	 */
    	TextViewer(Invocation _invoc, TextHandler _textHandler){
    		super(TextViewer.DEFAULT_TITLE);
    		
    		invoc = _invoc;
    		textHandler = _textHandler;
    		
    		this.addCommand(detailCommand);
    		this.addCommand(backCommand);
    		setCommandListener(this);
    		displayContent(invoc);
    	}
    
  4. Before the invocation, the invoc object and URL need to be validated.

    	/**
    	 * @param invoc Text Invocation instance.
    	 */
    	private void displayContent(Invocation invoc) {
    		
            String url = null;
            if ((invoc == null)) {
                this.append(TextViewer.NO_CONTENT);
                return;
            }
            
            if ((url = invoc.getURL()) == null){
            	this.append(TextViewer.NO_CONTENT);
                return;
            }
            
            // Set file name as a form title
            String title = url.substring(url.lastIndexOf('/')+1);
            this.setTitle(TextViewer.DEFAULT_TITLE+" - "+title);
    
            HttpConnection conn = null;
            InputStream input = null;
            StringBuffer txtContentBuff;
    
  5. Next you need to create a HTTP connection to get the text content and then append the content to the Form on the Display. The code below creates a StringBuffer for the text and then enters it into the Form.

    try {
            	//Open connection using invocation
                conn = (HttpConnection)invoc.open(false);
                conn.setRequestMethod(HttpConnection.GET);
    
                // If not successful, Display response code with response message.
                if (conn.getResponseCode() != HttpConnection.HTTP_OK) {
                    this.append(conn.getResponseMessage() + " (" + conn.getResponseCode() + ")");
                    return;
                }
    
                // Create StringBuffer with the size of response length
                txtContentBuff = new StringBuffer((int)conn.getLength());
    
                // Max size supported is char 4096;
                int maxLen = 4096;
                input = conn.openInputStream();
                // Read char and store it to the StringBuffer
                int ch = 0;
                while ((ch = input.read()) != -1) {
                    txtContentBuff.append((char)ch);
    
                    if (txtContentBuff.length() >= maxLen) {
                        break;
                    }
                }
    
                // Process the buffer and append text to the form
                this.append(new StringItem(null, txtContentBuff.toString(), Item.PLAIN));
                
            } catch (Throwable th) {
            	
                txtContentBuff = null;
                th.printStackTrace();
            } finally {
                try {
                    if (input != null) {
                        input.close();
                    }
    
                    if (conn != null) {
                        conn.close();
                    }
                } catch (Exception e) {}
            }
    	}
    
  6. Add Command handling functions for the choices.

    	public void commandAction(Command command, Displayable disp) {
    		if (command == detailCommand){
    			try {
    				String details = "";
    				details = "Invocation Type: "+invoc.findType()+ "\n";
    				details += "Action: "+invoc.getAction()+ "\n";
    				details += "ID: "+invoc.getID()+ "\n";
    				details += "Invoking App Name: "+invoc.getInvokingAppName()+ "\n";
    				details += "Invoking Authority: "+invoc.getInvokingAuthority()+ "\n";
    				details += "Invoking ID: "+ invoc.getInvokingID() +"\n";
    				
    				Alert alert = new Alert("Details",details,null,AlertType.INFO);
    				alert.setTimeout(Alert.FOREVER);
    				textHandler.display.setCurrent(alert);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}	
    		} else if (command == backCommand) {
    			
    			textHandler.doFinish(Invocation.OK);
    			textHandler.display.setCurrent(textHandler.choices);
    		}
    	}
    }