Pratyush Kshirsagar
HomeMy MotherlandDragonFlyTimenoidCommon Page

Timenoid
Timenoid has various definitions.
A Timenoid is a "Clockwork Demon" or a "Clockwork Devil" or A Timenoid is an immortal, gothic creature that often resides in castles, manors or mansions. I felt this would be a good title for the page where I can put some useful material, which I worked on to utilize my time.

Sample Image Application to show Facebook Albums and Photos -

The knowledge that I needed for connecting with facebook, querying the facebook database, etc was gained by reading the below mentioned articels. The first url will take you to the IBM developers works website giving an introduction to the facebook api, which is explained in a very good way. The next two url's provide you with different ways to start building an app using facebook api's. Their are lot of simple tutorials floating around the net explaining "how to post a message on facebook wall".
So, going through the above mentioned links, we get the pieces to develop a preliminary application utilizing the facebook api's (sample facebook app). I will be providing the methods that are called to get the facebook albums and pictures from that albums. There are various request() methods for Old REST and Graph API calls. Also, there are numerous wrappers that make developers work easy. We can use Facebook Query Language (FQL) which enables us to use a SQL-style interface to query the data exposed by the Graph API.

The method getUserAlbums() will request for the information about the facebook albums such as album_id, name, date, etc. We are using the graph query but if we want to use the FQL we send the query as a bundle. We have a listener named albumRequestListener to process the response. If we used FQL to request for the data we need to put the obtained JSON response inside the {"data" <JSON RESPONSE> } the block so that it can be parsed, otherwise an error would be thrown stating "invalid response". Do not cover up the response for the graph query because the response is already well formatted.
For requesting the photos inside particular album send the query, offcourse you need a listener to process the request :
asyncRunner.request(albumID + "/photos" , new albumGalleryRequestListener());

/**
  *  Check the session is valid and send the request to get the album infotmation
  */
public void getUserAlbums() {
        if (facebook.isSessionValid()) {
                facebook.getAccessToken();
                asyncRunner = new AsyncFacebookRunner(this.facebook);
                /* send a request as a graph query */
                asyncRunner.request("me/albums" , new albumRequestListener());
                /* Below is the the mechanism to send request using FQL, it is being commented */
                //Bundle params = new Bundle();
                //params.putString("method", "fql.query");
                //params.putString("query","SELECT aid, name, object_id FROM album WHERE owner=me());               
                //asyncRunner.request(params, new albumRequestListener1());
        }
    }   
/**
 * Listen for the response from facebook and store the obtained data
 * into appropriate container
 */
private class albumRequestListener implements RequestListener {
        @Override
        public void onComplete(String response, Object state) {
            String id = null, name = null;
            try {
                Log.d(TAG, "Response: " + response.toString());
                //remove comment from the below line of code if FQL request sent
                //String response1 = "{\"data\":" + response + "}";
                JSONObject json = null;
                try {
                    // change the response to response1 if the FQL reqest was sent
                    json = Util.parseJson(response);

                }catch(JSONException x){
                    Log.w(TAG, "JSON Error in response");
                }
               
                JSONArray d = json.getJSONArray("data");
                int l = (d != null ? d.length() : 0);

                for (int i=0; i<l; i++) {
                    JSONObject o = d.getJSONObject(i);
                    try{
                    id = o.getString("id");
                    }catch(JSONException x){
                        Log.w(TAG, "JSON Error in response");
                    }
                    try{
                    name = o.getString("name");               
                    }catch(JSONException x){
                        Log.w(TAG, "JSON Error in response");
                    }
                    albums.add(new AlbumItem(id,name));
                }
                Intent Update = new Intent("AlbumUpdatedIntent");
                Update.setAction("AlbumUpdatedIntent");
                context.sendBroadcast(Update);  
            } catch (JSONException e) {
                Log.w(TAG, "JSON Error in response");
            } catch (FacebookError e) {
                Log.w(TAG, "Facebook Error: " + e.getMessage());
            }
        }

        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub
           
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
            // TODO Auto-generated method stub
           
        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
            // TODO Auto-generated method stub
           
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub
           
        }
 
    }


Here, are the images that show the login page, albums and photos.
You can download the sample app
here. The sample application was developed for experimental purpose and contains some minor bugs.
There is a bug which causes some authorization problems and the app fails silently. If you have facebook application already installed, please remove it prior to using the sample app. I have provided the facebook jar file in the libs folder please add it to compile and run application without failure.
You can mail me if you have problem installing the application, contact details are on the Home page.