Consuming Data Services via Java/Android

Introduction- In JAVA/Android, we don”t have any utility we can now use RESTLET framework to generate Entity Classes similarly to .NET or PHP. Depending on your preference, you can either follow the

1.Restlet Framework Method
2.Manual Method.

1.Restlet Framework Method- This method requires you to download these custom library files. After which, you need to follow the steps mentioned below.


First, copy the org directory from the downloaded custom library files to your java src directory

Next, add reference to libraries. They are namely

org.restlet.jar
org.restlet.ext.xml.jar
org.restlet.ext.freemarker.jar
org.restlet.ext.atom.jar
org.freemarker.jar

Run the Generator Class to generate entity and helper classes

Add authentication information

Executing the query to get back the dataset

The sample code for consuming data via Restlet for Java is now available.

2. Manual Method – The same idea as querying the Data Service from your Web Browser.

        //Creating a URLConnection.
    	URL url = new URL(“{Data Service URI Here}” + “[?$filter={filters here}(Optional)]“);
    
    	//Adding Authentication Information.
    	conn.addRequestProperty(“AccountKey”, “{Account Key Value Here}”);
    	conn.addRequestProperty(“UniqueUserID”, “{GUID Value Here}”);
    	//Executing the URLConnection and getting the result in Atom Feed format.
    	BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()),8000);
    	

    Oh, what about the XML now? How do i format the XML returned?

    Parsing Atom Feed using an easy(read lazy) way
    The properties of the Entity in the results is encapsulated by the ”<m:properties>” tag.
    Assuming that the atom feed is stored in a String variable myAtomFeed,
    we can get the entities by splitting by the string “<m:properties>”.

    //This will return you an array of String.
    myAtomFeed.split(“<m:properties>”)
    

    You can easily iterate through this array of String and get the properties values out by doing a getStringBetween() custom function to extract the values between the two string values found in the specified string.getStringBetween() source (for the lazy ones like me :P ):

    	public static String getStringBetween(String src, String start, String end)
    	{
    	StringBuilder sb = new StringBuilder();
    	int startIdx = src.indexOf(start) + start.length();
    	int endIdx = src.indexOf(end);
    	while(startIdx &lt; endIdx){
    		sb.append("" + String.valueOf(src.charAt(startIdx)));
    		startIdx++;
    		}
    	return sb.toString();
    	}
    	

    usage: if we have a string variable

    	myAtomFeed = "<d:CuisineType>Malay</d:CuisineType>"
    
    
    
    
    	 getStringBetween(myAtomFeed,"<d:CuisineType>","</d:CuisineType>");
    	 

    By executing this, we will get the string “Malay” back. :D This is as simple as that!- Once you have access to the property”s value, you can construct a class and create objects and store it inside an Array or ArrayList. :) So in short, you have just completed your simplest parser.

Querying Data Service (Manual Method)

This entry was posted in Uncategorized. Bookmark the permalink.

12 Responses to Consuming Data Services via Java/Android

  1. Pingback: Datasets – Singapore Land Transport Authority (LTA) « Project Nimbus

  2. Pingback: Datasets – Singapore National Environment Agency (NEA) « Project Nimbus

  3. Pingback: Datasets – HungryGoWhere (HGW) « Project Nimbus

  4. Pingback: Datasets – National Library Board (NLB) « Project Nimbus

  5. Pingback: Datasets – Singapore Post (SP) « Project Nimbus

  6. Pingback: Datasets – ShowNearBy (SNB) « Project Nimbus

  7. Pingback: Datasets – Cinema Online (CO) « Project Nimbus

  8. Pingback: Datasets – Singapore Tourism Board (STB) « Project Nimbus

  9. Pingback: Datasets – Chlkboard (CHKB) | Project Nimbus

  10. Pingback: Datasets – User Contributed Content (USR) | Project Nimbus

  11. Pingback: Datasets – Places.sg | Project Nimbus

  12. Pingback: Datasets – Chlkboard | Project Nimbus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s