Tag: javame

Implementing JSON in BlackBerry Application

JavaScript Object Notation (JSON) is a lightweight, low overhead, method of describing objects. This notation can be extremely useful when implemented as an alternative to web services though does typically require a bit more coding to handle how the custom data types are converted to and from JSON format. However, as this is controlled by code and JSON handles most primitive data types that are common among programming languages, it can also be used as a communication medium between systems and applications built on different programming languages. Libraries for various languages have already been created and are ready to implement. For more information on these please refer to the JSON home page. This site also contains lots of background information about JSON and some source code.

The sample attached below shows a basic cake ordering system that helps construct a CakeObject that it then converted into a JSON String. This example will help familiarize you with the steps required to implement JSON in your own application as well as determine the benefit of its use and the way different data types are formatted once converted to JSON. It contains two projects, the application and the JSON ME library source code. These will need to be set up so that the application depends on the library in order to compile and test.

[1] http://supportforums.blackberry.com/t5/Java-Development/Sample-Code-Implementing-JSON-in-your-application/ta-p/539659
[2] https://meapplicationdevelopers.dev.java.net/source/browse/meapplicationdevelopers/demobox/mobileajax/lib/json/src/org/json/me/

jsonlib_javame__download_pdf_and_renameto_zip
JSONDemoApp__download_pdf_renameto_zip


BlackBerry (JavaME) String Manipulation – split, replace, replaceAll

The String class in the BlackBerry API set is defined on the J2MEĀ® specification, which doesn’t include all methods available in J2SE. RIM has provided a StringUtilities class that contains some of the J2SE methods. This article provides some additional methods that aren’t included in the String and StringUtilities classes.

public String[] split(String strString, String strDelimiter)
	{
		int iOccurrences = 0;
		int iIndexOfInnerString = 0;
		int iIndexOfDelimiter = 0;
		int iCounter = 0;

		// Check for null input strings.
		if (strString == null)
		{
			throw new NullPointerException("Input string cannot be null.");
		}
		// Check for null or empty delimiter
		// strings.
		if (strDelimiter.length() <= 0 || strDelimiter == null)
		{
			throw new NullPointerException("Delimeter cannot be null or empty.");
		}

		// If strString begins with delimiter
		// then remove it in
		// order
		// to comply with the desired format.

		if (strString.startsWith(strDelimiter))
		{
			strString = strString.substring(strDelimiter.length());
		}

		// If strString does not end with the
		// delimiter then add it
		// to the string in order to comply with
		// the desired format.
		if (!strString.endsWith(strDelimiter))
		{
			strString += strDelimiter;
		}

		// Count occurrences of the delimiter in
		// the string.
		// Occurrences should be the same amount
		// of inner strings.
		while((iIndexOfDelimiter= strString.indexOf(strDelimiter,iIndexOfInnerString))!=-1)
		{
			iOccurrences += 1;
			iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
		}

		// Declare the array with the correct
		// size.
		String[] strArray = new String[iOccurrences];

		// Reset the indices.
		iIndexOfInnerString = 0;
		iIndexOfDelimiter = 0;

		// Walk across the string again and this
		// time add the
		// strings to the array.
		while((iIndexOfDelimiter= strString.indexOf(strDelimiter,iIndexOfInnerString))!=-1)
		{

			// Add string to
			// array.
			strArray[iCounter] = strString.substring(iIndexOfInnerString, iIndexOfDelimiter);

			// Increment the
			// index to the next
			// character after
			// the next
			// delimiter.
			iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();

			// Inc the counter.
			iCounter += 1;
		}
            return strArray;
	}
public String replace(String source, String pattern, String replacement)
	{

		//If source is null then Stop
		//and return empty String.
		if (source == null)
		{
			return "";
		}

		StringBuffer sb = new StringBuffer();
		//Intialize Index to -1
		//to check against it later
		int idx = -1;
		//Intialize pattern Index
		int patIdx = 0;
		//Search source from 0 to first occurrence of pattern
		//Set Idx equal to index at which pattern is found.
		idx = source.indexOf(pattern, patIdx);
		//If Pattern is found, idx will not be -1 anymore.
		if (idx != -1)
		{
			//append all the string in source till the pattern starts.
			sb.append(source.substring(patIdx, idx));
			//append replacement of the pattern.
			sb.append(replacement);
			//Increase the value of patIdx
			//till the end of the pattern
			patIdx = idx + pattern.length();
			//Append remaining string to the String Buffer.
			sb.append(source.substring(patIdx));
		}
		//Return StringBuffer as a String

                if ( sb.length() == 0)
                {
                    return source;
                }
                else
                {
                    return sb.toString();
                }
	}
public String replaceAll(String source, String pattern, String replacement)
{

    //If source is null then Stop
    //and retutn empty String.
    if (source == null)
    {
        return "";
    }

    StringBuffer sb = new StringBuffer();
    //Intialize Index to -1
    //to check agaist it later
    int idx = -1;
    //Search source from 0 to first occurrence of pattern
    //Set Idx equal to index at which pattern is found.

    String workingSource = source;

    //Iterate for the Pattern till idx is not be -1.
    while ((idx = workingSource.indexOf(pattern)) != -1)
    {
        //append all the string in source till the pattern starts.
        sb.append(workingSource.substring(0, idx));
        //append replacement of the pattern.
        sb.append(replacement);
        //Append remaining string to the String Buffer.
        sb.append(workingSource.substring(idx + pattern.length()));

        //Store the updated String and check again.
        workingSource = sb.toString();

        //Reset the StringBuffer.
        sb.delete(0, sb.length());
    }

    return workingSource;
}

[1] http://supportforums.blackberry.com/t5/Java-Development/String-Manipulation-split-replace-replaceAll/ta-p/620038


  • Copyright © 1996-2010 vaks.in. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress