Monday, October 24, 2011

On JSON and Rest Requests in Android


Yet another shamelessly lifted answer from SO that I wrote
http://stackoverflow.com/questions/6218143/android-post-json-using-http/6218563#6218563

using example from here credits to Justin Grammens at this blog for original post http://localtone.blogspot.com/2009/07/post-json-using-android-and-httpclient.html
About JSON

JSON is a Javascript Object Notation,

//In javascript properties can be referenced both like this
object1.name
//and like this
object['name'];

the example from the article used this bit of JSON
The Parts

a fan object with email as a key and foo@bar.com as a value
{ fan: { email : 'foo@bar.com' } }

so the object equivalent  would be fan.email; or fan['email'];

both would have the same value of 'foo@bar.com'
About HttpClient Request

The following is what our author used to make a [HttpClient Request][1]. I do not claim to be an expert at all this so if anyone has a better way to word some of the terminology feel free.

java map reference


   1: public static String makeRequest(String path, Map params)throws Exception 
   2: {
   3:     //map is similar to a dictionary or hash
   4:  
   5:     //instantiates httpclient to make request
   6:     DefaultHttpClient httpclient = new DefaultHttpClient();
   7:  
   8:     //url with the post data
   9:     HttpPost httpost = new HttpPost(path);
  10:  
  11:     //all the passed parameters from the post request
  12:     //iterator used to loop through all the parameters passed in the post request
  13:     Iterator iter = params.entrySet().iterator();
  14:  
  15:     //Stores JSON
  16:     JSONObject holder = new JSONObject();
  17:  
  18:     //using the eariler example your first entry would get email and the inner while would get the value which would be 'foo@bar.com' 
  19:     { fan: { email : 'foo@bar.com' } }
  20:  
  21:     //While there is another entry
  22:     while(iter.hasNext()) 
  23:     {
  24:         //gets an entry in the params
  25:         Map.Entry pairs = (Map.Entry)iter.next();
  26:  
  27:         //creates a key for Map
  28:         String key = (String)pairs.getKey();
  29:  
  30:         //Create a new map
  31:         Map m = (Map)pairs.getValue();   
  32:  
  33:         //object for storing Json
  34:         JSONObject data = new JSONObject();
  35:  
  36:         //gets the value
  37:         Iterator iter2 = m.entrySet().iterator();
  38:         while(iter2.hasNext()) 
  39:         {
  40:             Map.Entry pairs2 = (Map.Entry)iter2.next();
  41:             data.put((String)pairs2.getKey(), (String)pairs2.getValue());
  42:         }
  43:  
  44:         //puts email and 'foo@bar.com'  together in map
  45:         holder.put(key, data);
  46:     }
  47:  
  48:     //passes the results to a string builder/entity
  49:     StringEntity se = new StringEntity(holder.toString());
  50:  
  51:     //sets the post request as the resulting string
  52:     httpost.setEntity(se);
  53:     //sets a request header so the page receving the request will know what to do with it
  54:     httpost.setHeader("Accept", "application/json");
  55:     httpost.setHeader("Content-type", "application/json");
  56:  
  57:     //Handles what is returned from the page 
  58:     ResponseHandler responseHandler = new BasicResponseHandler();
  59:     response = httpclient.execute(httpost, responseHandler);
  60: }
  61:  

Please feel free to comment on any questions that arise about this post or if I have not made something clear or if I have not touched on something that your still confused about... etc whatever pops in your head really.

(I will take down if Justin Grammens does not approve. But if not then thanks Justin for being cool about it.)

Resources abound beginning with my own answer to a similar question.

Post json using http
A basic Rest API client example
How to do a restful post call.
Rest API with http authentication



No comments: