Meet JSON !

Ever wondered about ways to connect users of your Android App through the internet with less difficulty in data manipulation? One way would be developing the application fully as a web app and view it on a WebView. But sometimes you may still want to have implementations using its native features. And JSON can help you with that. Well, not in the process of data streaming but in easy data manipulation.

JavaScript passing Post data to PHP via AJAX

$.post(url, 
       {action:"register", params:"params"}, 
       function(data) {
             console.log(data);
});

I have been using JSON objects to be passed through the ajax call like the above jQuery code, for quite some time and that’s it. One time I became so curious about this technology about why I see it everywhere. If JSON can be passed between JS and PHP, would it be possible if I use it within Android and send ’em to a PHP Server code? Then I visited JSON official site

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

– JSON.org

And I said to myself… “OOOOHHHHHH! this means big for my coding convention”. Immediately, ideas came passing through my mind of a lot of possibilities of my future projects. I now use JSON widely across all my projects in PHP, Android, and plainly Java and I want to share it with you.

This blog entry will not be focusing on the technical details of what JSON is or how its data structure was designed and how. Those details can be acquired by going to the official website. The purpose of this entry is to invite non JSON users to pretty much follow the trend.

Where is JSON used in today’s mainstream tech? It’s everywhere ! ! !

Not to mention, Youtube, flickr, Facebook, Google+, and the list goes on.

So What Are The Possibilities If I use JSON?

A lot, basically. One of it is that you could make a RESTful Web API. But because I Love Android, I would focus in Android realm.

In this one I would like to show an implementation using Android.

JAVA (CLIENT)


	HttpClient client = new DefaultHttpClient();
	HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);

        HttpPost post = new HttpPost("http://localhost/jsonTest"); 
                                      // or whatever url you have to your chosen folder
        
        HttpResponse httpResponse = client.execute(post);

        // catch whatever the server prints
        String response = org.apache.http.util.EntityUtils.toString(httpResponse.getEntity());
        
        // to be continued
     /* ... */

PHP (SERVER)

        $userInfo = array("first_name"=>"john", "last_name"=>"doe");
        echo json_encode($userInfo); // outputs as string

JAVA

Going back to our Java code, we can then instantiate a JSONObject for the caught JSON string

     /* ... */
     JSONObject resFromServer = new JSONObject(response);

     // AND THEN...
     Log.i("user first and last name", 
                       resFromServer.getString("first_name")
                       +" "+resFromServer.getString("last_name"));

We can now get any data within the object by calling the method .getString() for strings, .getInt() for Integer data type, getDouble, getBoolean, and more depending on what data type you want to retrieve.

But of course, these codes alone WILL NOT work. BUMMER.

Android would only allow you to call HTTP methods using an object that inherits AsyncTask. For a complete tutorial on how to send and retrieve JSON data from and to Android, read Sending JSON Data From Android to a PHP Script, which for now is still on draft.

Leave a comment