Monday, October 12, 2009

jSQL release 2

I'm happy to announce release 2 of jSQL, the much easy-to-use JavaME (or J2ME) wrapper library for Record Management Store(RMS).

Release Notes:
1. Refactored code for optimization
2. Fully documented

Download packages:

Working with Facebook Platform

I had a really difficult time trying to develop a simple page on the Facebook platform. It demanded lots of time and effort. It was like i was in a whole new world of re-organised programming platform. Talk about Facebook Markup Language, Facebook Query Language, Facebook JavaScript, the API itself,  etc.

You can get really knocked out as to why they have to define their own way for programmers to follow. But hey, i respect their architecture. You can't just play around with people's publicly personal accounts. (lol!!).

After about 3 weeks of battling with the platform, i finally got the modus operandi. As i always do, when i get knowledge, i share.

I've written this post to guide newbies (or dummies, if you're one) on how to create a Facebook Application.

As usual, i assume you already have some basic skills in programming especially Javascript, PHP, or even the ASP.NET languages (C#, VB.NET).

Ok, let's start. I'm going to do 6 things.
1. Briefly introduce you to the Facebook platform
2. Crawl with you through the pre-requisites
3. Walk you you through your application setup and settings
4. Run with you through an example code
5. Fly with you through 1 or 2 high level intricacies.
6. Kick you outta here to start your own application.

1. Intro
Facebook's developer platform enables developers to use any programming language to query their API via REST calls. As such, you can develop both desktop and web applications to do practically the same thing; update status, comment on status, view friend's profiles, it even goes to reading mails, uploading multimedia and send SMS. Awesome init?

2. Let's crawl
Of course, you need to have a Facebook account before you can develop/host your application on their framework. So, after you login, go to the bottom of the page and click on 'Developers'.


3. Walk with me:
Are you on the developer's page? Ok, now click on 'Start Building for your site'. 


A wizard-like setup page appears. Enter your details as shown. Click 'Next Step'


You can safely ignore step 2 (Try it if you're that curious)
That's it.
Do you see a page similar to the one shown below?


Great!! Copy your API Key and Secret. They are the the lifeblood of your application. Click on 'Developer Dashboard'. Allow access to the next page. As you might a little confused, the page you are entering is in itself a Facebook Application. It is a sort of control panel for all your applications (yeah, i said applications; you can setup more than 1).
Ok, now click your application name. (shaded yellow in the image below)

The next page is the actual control panel for your new application.Click on 'Edit Settings'.

The next page presents you with a host of options. You can perform 1001 customizations on your application. Go through it, be curious!! One important page is on  'Canvas'.

The portions shaded yellow are those i deem important. The first textfield requires you to specify just a URL name for your application. The next textfield is where you have to enter the exact URL to your index page (e.g. http://www.mytextApp.com). This page would be automatically loaded to the Facebook platform when a user visits your application.
The next step is to specify the render method. As far as i know, Facebook renders your application as either iFrame or FBML. I like FBML;it enables you to use FBJS and FBML tags that would be intepreted and nicely rendered on your page. Read more here. Save your changes.

4. Ready to run?
Honestly writing, the only example i got was on the main dashboard. Click on 'Back to my Applications' at the top. Scroll down and click on the hyperlinked text 'example code'.


This pops up a dialog containing PHP codes. Yipeeeee!! Are we done? NO!! When you inspect the code, do you see 'require_once facebook.php' and a line of code that reads $facebook = new Facebook($appapikey, $appsecret);
Where did they get that? It's a PHP client library and it's freeeee!!. You can get on the same page. Scroll down and click on 'Download the Client Library'.
What about .NET folks? Get yours here.
Java lovers, try here


5. Let's fly
Be patient!! Facebook offers high-level security to their account holders. Before you can do anything, the user needs to grant permission for your application. That permission can either be temporal or permanent.
Let me explain.
Anytime a user visits your page, Facebook transfers a session ID to your application. You can use this to get user's status update, list friends, groups or events. You can even use this to access the user's account offline. However, the user again needs to grant that permission; and this means you have to save it to a database.
Read on permissions here.
Get more info on using session IDs here or there.


6. I encourage you to be more curious. Play with the platform. It's fun.

Tuesday, September 8, 2009

Introducing jSQL...RMS simplified

Working with Record Management Store (RMS) on JavaME can exhaust man hours if you are not so conversant with its operations. You may end up not storing anything at all or finding it hard to update your data.

I want to introduce you to jSQL, a powerful, lightweight JavaME library purposefully written to ease your burden when working with RMS. And guess what? IT IS FREEE!!!!...but source is not available till further notice.

Facts

  1. physical size: 5KB
  2. Platform: JavaME
  3. IDEs: any JavaME supported IDEs such as Eclipse, NetBeans
  4. Performance: superb....unlike any other

You can download zip file containing jSQL jar file, source documentation in .zip format and a sample code file.

Release notes

Version 1.0.1 - September 2009
first public release
has methods to insert, update, delete or read data from RMS.
very lightweight, simple and easy to use.

Thursday, August 20, 2009

Make your MIDlets Eventful

This article shows you how to create custom events on the JavaME platform.

It's as easy as my other article on .NET custom events.

Events are needed so as to display information on threaded (background) process that cannot be readily rendered on the UI controls. An example is sending SMS. The user may want to know 'what is going on?' as against how it is done.

I'll use the same example in my previous article on SMS.

Let's box on then!

First we need an event class. This acts like the messaging item that would be read from the UI.

So we do:
public class MessageSenderEvent{

public MessageSenderEvent(String msg){
currentStatus = msg;
}

public String currentStatus;

}

Next we need the event listener interface. This has to be implemented in our UI to listen and display status updates from the MessageSender class.

So,

public interface MessageSenderEventListener{
public void statusUpdated(MessageSenderEvent e);
}

Third, we need to modify our MessageSender class.
So,


import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
* Class to send SMS
* @author Augustine
*
*/
public class MessageSender {
private MessageSenderEventListener listener = null;

public void setMessageSenderEventListener(MessageSenderEventListener alistener){
this.listener = alistener;
}

protected void onStatusUpdated(MessageSenderEvent e){
if(listener!=null){
//invokes the method on the interface...
listener.statusUpdated(e);
}
}
private String themessage;
private String destination;
private MessageConnection conn;
/**
* Sends an SMS.
* The destination number is retrieved internally from AdminSettings class
* @param message The message to be sent. The message prefix would be appended to it.
*/
public void sendMessage(String message,String destination){
themessage = message;
destination = destination;
onStatusUpdated(new MessageSenderEvent("Beginning..."));
Thread msgThread = new Thread() {
public void run() {

try {
// sets address to send message
System.out.println(destination);
System.out.println("Message: " + themessage);
String addr = "sms://" +destination + ":2000";
// opens connection
onStatusUpdated(new MessageSenderEvent("Establishing connection..."));
conn = (MessageConnection) Connector.open(addr,2);

onStatusUpdated(new MessageSenderEvent("Connection established..."));
// prepares text message
TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
// set text
msg.setPayloadText(themessage);

onStatusUpdated(new MessageSenderEvent("Preparing to send..."));
// send message
conn.numberOfSegments(msg);
onStatusUpdated(new MessageSenderEvent("Sending..."));
conn.send(msg);//sends the message

onStatusUpdated(new MessageSenderEvent("Message sent...\nClosing connection..."));
conn.close();//closes the connection

onStatusUpdated(new MessageSenderEvent("Connection closed..."));

} catch (Exception e) {
onStatusUpdated(new MessageSenderEvent("An error has occured...\n" + e.getMessage()));
}
finally{
try {
if (conn!=null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}

}
};

msgThread.setPriority(Thread.NORM_PRIORITY);
msgThread.start();
}
}

As you can see, we created an instance of our interface, and provided a method (setMessageSenderEventListener(...)) to set the listener to an instance.

Finally, we can work off something...

We can now do this in our UI:

public class MyMIDlet extends MIDlet implements MessageSenderEventListener{
public MyMIDlet(){
}

public void startApp(){
try{
MessageSender sender = new MessageSender();
sender.sendMessage("hello world","+233123456789");
sender.setMessageSenderEventListener(this);
}
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){}

//overriden method in interface

public void onStatusUpdated(MessageSenderEvent e){
//code goes here for displaying status updates
System.out.println(e.currentStatus);
}
}

And were done!!

Enjoy.

Is this blog great?