Thursday, January 13, 2011

Consuming REST Web Service using Java and Jersey API

In this tutorial we will create a Java application which will consume REST Web Service.

The web service which will be consumed is developed in this post.


If you have created the Web Service that means you already download Jersey API.
We will need those API for our application.


Create a new java project. Name it "NoteJava"


Insert those Jersey API libraries in our build path.


Create a new java class. Name it "NoteJava.java"




Run the application.
Console will print xml data which you can use and parse for your application.

Its very simple!

Consume REST Web Service using iPhone

In this tutorial we will create an iPhone application which will consume REST Web Service.

The web service which will be consumed is developed in this post.

Our final application should looked like this.
This note is taken from a REST web service which taken its data from a database server.

First, create a new Window Based Application. Use "Notes" as the project name.

Next, create a new UIViewController subclass, check the UITableViewController subclass in the options panel but leave With XIB for user interface unchecked.
Use "NotesTableViewController" as the name.
Create several variables and a method in NotesTableViewController.h



Then, we will implement the method in NotesTableViewController.m



Create another UIViewController subclass for handling new note insertion. This time, uncheck the UITableViewController subclass but check the With XIB for user interface. Use "NotesInsertViewController" as the name

Next, create some outlet and a method in our controller. Modify "NotesInsertViewController.h"



Then, double click NotesInsertViewController.xib to open Interface Builder.

Create two UITextField and one Rounded Button.
Connect both UITextField with the appropriate field: idField and contentField
Connect Rounded Button with the insertNote action

Next, implement the method in NotesInsertViewController.m



After this we can create the tab controller to navigate between the view table and insert view
Open and edit NotesAppDelegate.h



Create a UITabBarController and assign each tab item with the appropriate controller. Open and edit NotesAppDelegate.m



Build & run our application.
Notice that in inserting a new note we hardcode the date as a string and put 8 which will be interpreted by the web service as "8 seconds after 1 January 1970"

It is a little buggy and the view might not as good. But the point of the tutorial is on consuming REST Web Service, so i decide to make a minimum design and focus on the methods for handling Web Service.

Anyways, enjoy playing with REST Web Service!

Consuming REST Web Service using Android

In this tutorial, we will find out how to create an Android application to consume REST Web Service.

We will create android application which will consume web service in this post .
Our application should look like this.

Create a new Android Application named "Note Android".
Modify AndroidManifest and add INTERNET permission.

Next, create the view for posting new note : main.xml



And the view for viewing all of our notes. There is no component here. Only a layout because we will create the component programmatically



Next, create our view for posting note : DatabaseAndroid.java



And the code for parsing xml retrieved from web service and present each element on a TextView : ViewDatabase.java



Run our application and try creating new notes.

Have fun!

Wednesday, January 12, 2011

Creating REST Web Service using Java and Jersey API

In order to create a RESTful Web Service using Java, we need several tools and library
The tools and library which i used are:
1. Java 1.6
2. Eclipse IDE for Java EE developers (Ganymede)
3. Apache Tomcat (6.0)
4. Jersey API (1.4) . Download the zipped file.
5. Oracle Database 10g or any other DBMS

Create a new DynamicWebProject in Eclipse. If no such project template exists, you should download Eclipse for Java EE developers.
Copy those Jersey API librari in "WEB-INF\lib" folder
Modify web.xml to register our jersey servlet. Put any name on the servlet name tag. It doesn't matter.



Our web service will connect to database and retrieve or insert a new note in NOTE table
Create a new table named : NOTE with the following columns
NOTEID       : number
CONTENT   : varchar
CREATEDDATE : date

We will create a model class for that table: Note.java



Next, create a class that will handle connection with database: DatabaseAccess.java



After that, create a class that will handle the database operation. A Data Access Object :
NoteDao.java



Finally, we create a resource class that will be able to retrieved by the client using REST web service



Save and deploy the project.
Populate our NOTE table using some dummy data and try to call our resource using the following URL in your web browser.

http://localhost:8080/NoteWS/notes

If u got xml file containing the records in your NOTE table. You have successfully create a REST Web Service.

Now, how can we use the POST method to actually insert a record.
Well, we can create a html form whose method is POST and submit action refers to

http://localhost:8080/NoteWS/notes

Something like this:
Notice that we have to make sure our text field name match the form parameter name defined in NoteResource.java



Note that createddate accept a String but in NoteResource.java it parses the string to primitive type long for creating new Date object. So, for now, just insert any number which will represent the second from January 1, 1970.

access http://localhost:8080/NoteWS/notes again to check our new note.

That is if for creating REST Web Service using Java and Jersey API.

Check out my other tutorial to find out how to consume this REST Web Service From
1. Simple java project
2. Android
3. iPhone

Have fun with REST!

Create List without extending ListActivity

Creating list view in Android is an easy task.
All we have to do is extending ListActivity, implement the methods and we are good to go.
But there are times when it is not an option, because we need to extend another class.
Let say....MapActivity? So we need to create a two tabbed view for user to be able to change from map view to list view. Our intended application may look like this.

When user click on an entry in list view, user will be redirected to the entry location in map view.
In this tutorial, we will create only a ListView then, we will add Tabs and MapView Tab.
We will need to create our implementation of ArrayAdapter and override its getView() method to accomodate our own view. We will create a title, a description and an icon for each row.

Dont forget to add uses library and internet permission in AndroidManifest.xml

First, we need to create a model class. Let say Restaurant.java



After that, we will create our view: main.xml



Next, create our layout for each row in our list view: row.xml



Next step, our main application view: DoubleTabView.java



Execute the application and we will see our generic list view application



Next, we will create a tab bar for user to choose List View or Map View and we will add the feature to navigate user to the map view whenever he/she click on a row in the List View

First, we should change our main.xml. We add TabHost, TabWidget and MapView



Next, we add the tab name programatically, then we add navigateToLocation method to navigate and zoom to the restaurant location



Build and run.
Our final application should look like this
Click one restaurant, and the application will navigate and zoom to the location

That's it! Have fun!





Create a Radius Around a Point in Google Map

Question : How do I create a 9000 meters buffer radius from a point and display placemarks that intersect that buffer.
Answer : Google Map API for android has a class that reads and draw your current location in the map.
That class is "MyLocationOverlay". The class has a "draw" method that will be called everytime user pan or zoom on the map.
So to draw a circle radius of your current location, we need to extends this class and override its draw method.

Then, we create our view, one map view, one textbox and one button


After that, we need to create our main class. TukangAndroidApp.java

Dont forget to edit AndroidManifest.xml to add permissions and google map library


Run your application.
Tips : Set the GPS location from the DDMS first before you press Search, otherwise you will encounter NullPointerException
If all goes well, your application will be like this

Notice in TukangAndroidApp.java, our displayResult method didnt do anything, so we will implement the method to return some placemarks that intersect our buffer.
First, we should extends ItemizedOverlay class to create our placemark


Next, we will implement the displayResult method.
Our last TukangAndroidApp.java should look like this

Run the application, and you can see that our application will work well.







Have fun!