Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Retrieve More of a Message

by Retired ‎02-16-2010 01:12 PM - edited ‎02-17-2010 03:56 PM (2,324 Views)

Summary


This article applies to the following:

  • BlackBerry® devices based on Java® technology


Details


When a message is initially delivered to a BlackBerry device, the entire message may not be available. Message content up to a maximum of 2 Kb is delivered to the BlackBerry device to conserve bandwidth and storage space. To obtain more of a message the Transport.more(BodyPart, boolean)method can be used. The second boolean parameter specifies whether the next 2 Kb of the message is requested (false) or if the complete message has been requested (true).


Note: The maximum size of a message is 32 Kb. Message contents beyond 32 Kb will be truncated. With BlackBerry® Device Software 4.0 and later, the BlackBerry® Internet Service supports complete message retrieval at once. That is, the boolean parameter can be set to true to retrieve the entire message. For BlackBerry Device Software versions prior to 4.0, the BlackBerry Internet Service does not support complete message retrieval at once; therefore, if the boolean parameter is set to true, the request is ignored by the BlackBerry Internet Service.


The following sample application illustrates how to retrieve more of a message:



/**

* RetrieveMore.java
*
*/

package com.samples.moreSample;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;

class RetrieveMore extends UiApplication implements MessageListener
{
private int messageID;

public static void main(String[] args)
{
RetrieveMore theApp = new RetrieveMore();
theApp.enterEventDispatcher();
}

public RetrieveMore()
{
MainScreen mainScreen = new MainScreen();

//A MenuItem to scan messages and request more.
MenuItem getMessage = new MenuItem("Get Message", 40, 40)
{
public void run()
{
getMessage();
}
};

//Set the title of the screen.
mainScreen.setTitle(new LabelField("More Sample"));

//Add the menu item to the screen.
mainScreen.addMenuItem(getMessage);

//Display the main screen
pushScreen(mainScreen);
}

//Locate a message that has more available and request more.

private void getMessage()
{
Store store = null;
Folder folder = null;

try
{
//Get the default mail session.
store = Session.waitForDefaultSession().getStore();
}
catch (NoSuchServiceException ex)
{
//No message services found!
System.out.println(ex.toString() + ": " + ex.getMessage());
}

try
{
//Open the Inbox for the default mail session.
//Open either the BWC or BES Inbox.
//BWC Inbox
//folder = store.getFolder("Web Client/Inbox");
//BES Inbox
folder = store.getFolder("Inbox");
}
catch (FolderNotFoundException ex)
{
//The Inbox was not found!
System.out.println(ex.toString() + ": " + ex.getMessage());
}

Message[] msgs = null;

try
{
//Get all messages from the Inbox.
msgs = folder.getMessages();
}
catch (MessagingException ex)
{
System.out.println(ex.toString() + ": " + ex.getMessage());
}

BodyPart bp;
boolean moreMessage = false;

//Loop until we find a message with more available.
for (int count = 0; count < msgs.length; ++count)
{
bp = (BodyPart)msgs[count].getContent();
if (( bp.hasMore() ) && (!bp.moreRequestSent()))
{
//A message with more available has been found!
moreMessage = true;
//Save the messageID
messageID = msgs[count].getMessageId();

System.out.println("The body size of the message before more is: "
+ getBodyContent(bp).length());

//getBodyText can also be used to retreive a String based body of a message.
System.out.println("The body size of the message before more is: "
+ msgs[count].getBodyText().length());

System.out.println("Requesting more for MessageID " + messageID);

//Request more, specify true in the second parameter to request the rest of
//the message body (up to a maximum of 32 kb).
//Note that BWC does not support the true parameter of a more request.
try
{
Transport.more(bp, true);
}
catch (MessagingException ex)
{
System.out.println(ex.toString() + ": " + ex.getMessage());
}

//Add a message listener to this message so the application will
//receive notification when the message has been changed.
msgs[count].addMessageListener(this);
break;
}
}

if (!moreMessage)
Dialog.alert("No messages with more available were found in the Inbox.");
}

//Returns a String obtained from the contents of all BodyParts
//of a Multipart message.
private String getBodyContent(BodyPart bp)
{
//Get the parent Multipart of the BodyPart of this message.
Multipart mp = bp.getParent();
StringBuffer sb = new StringBuffer();

//Loop through all child BodyParts of the Multipart.
for (int mpCount = 0; mpCount < mp.getCount(); ++mpCount)
{
//Assuming the message is text based TextBodyPart can be used.
TextBodyPart tp = (TextBodyPart)mp.getBodyPart(mpCount);
//Append the content of each BodayPart to a StringBuffer.
sb.append((String)tp.getContent());
}
return sb.toString();
}

//Invoked when a Message with a MessageListener has been changed.
public void changed(MessageEvent eve)
{
final Message msg = eve.getMessage();
System.out.println("*Message event for messageID: " + msg.getMessageId());
//Ensure the message is the same one that was recorded above.
if (eve.getMessageChangeType() == MessageEvent.UPDATED
&& msg.getMessageId() == messageID)
{
System.out.println("More has been received!");

BodyPart bp = (BodyPart)msg.getContent();
System.out.println("The body size of this message after more is: "
+ getBodyContent(bp).length());

//getBodyText can also be used to retreive a String based body of a message.
System.out.println("The body size of this message after more is: "
+ msg.getBodyText().length());
}
}
}




Contributors
Users Online
Currently online: 4 members 2,751 guests
Recent signins:
Please welcome our newest community members: