Monday, October 24, 2011

On JSON and Rest Requests in Android

Yet another shamelessly lifted answer from SO that I wrote

using example from here credits to Justin Grammens at this blog for original post
About JSON

JSON is a Javascript Object Notation,

//In javascript properties can be referenced both like this
//and like this

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

a fan object with email as a key and as a value
{ fan: { email : '' } }

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

both would have the same value of ''
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
   5:     //instantiates httpclient to make request
   6:     DefaultHttpClient httpclient = new DefaultHttpClient();
   8:     //url with the post data
   9:     HttpPost httpost = new HttpPost(path);
  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();
  15:     //Stores JSON
  16:     JSONObject holder = new JSONObject();
  18:     //using the eariler example your first entry would get email and the inner while would get the value which would be '' 
  19:     { fan: { email : '' } }
  21:     //While there is another entry
  22:     while(iter.hasNext()) 
  23:     {
  24:         //gets an entry in the params
  25:         Map.Entry pairs = (Map.Entry);
  27:         //creates a key for Map
  28:         String key = (String)pairs.getKey();
  30:         //Create a new map
  31:         Map m = (Map)pairs.getValue();   
  33:         //object for storing Json
  34:         JSONObject data = new JSONObject();
  36:         //gets the value
  37:         Iterator iter2 = m.entrySet().iterator();
  38:         while(iter2.hasNext()) 
  39:         {
  40:             Map.Entry pairs2 = (Map.Entry);
  41:             data.put((String)pairs2.getKey(), (String)pairs2.getValue());
  42:         }
  44:         //puts email and ''  together in map
  45:         holder.put(key, data);
  46:     }
  48:     //passes the results to a string builder/entity
  49:     StringEntity se = new StringEntity(holder.toString());
  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");
  57:     //Handles what is returned from the page 
  58:     ResponseHandler responseHandler = new BasicResponseHandler();
  59:     response = httpclient.execute(httpost, responseHandler);
  60: }

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: