Wednesday, May 15, 2013

Android GSON parsing


Step 1.
Download the Gson library from http://code.google.com/p/google-gson/ and add it to your java build path in eclipse.

Step 2.
Create a php file and paste the following json data into it

{"as_of":"Thu, 25 Feb 2010 11:30:17 +0000","trends"
:[{"name":"#nowplaying",
"url":"http://search.twitter.com/search?q=%23nowplaying"}
,{"name":"#nothingworsethan",
"url":"http://search.twitter.com/search?q=%23nothingworsethan"},
{"name":"Dubai Mall",
"url":"http://search.twitter.com/search?q=%22Dubai+Mall%22"},
{"name":"iPad Gets",
"url":"http://search.twitter.com/search?q=%22iPad+Gets%22"},
{"name":"#SuperJuniorTrot",
"url":"http://search.twitter.com/search?q=%23SuperJuniorTrot"},
{"name":"Justin Bieber",
"url":"http://search.twitter.com/search?q=%22Justin+Bieber%22"},
{"name":"Click",
"url":"http://search.twitter.com/search?q=Click"},
{"name":"Jaebum",
"url":"http://search.twitter.com/search?q=Jaebum"},
{"name":"#tosavemoney",
"url":"http://search.twitter.com/search?q=%23tosavemoney"},
{"name":"Protection",
"url":"http://search.twitter.com/search?q=Protection"}]}

Step 3.
Create the following two classes to hold the information of the json data

TwitterTrend .java

public class TwitterTrend {
    public String name;
    public String url;
}

TwitterTrends .java

public class TwitterTrends {

    public String as_of;
    public List<TwitterTrend> trends;
    
    public String getAs_Of() {
        return as_of;
    }
    public void setAs_Of(String asOf) {
        as_of = asOf;
    }
    public List<TwitterTrend> getTrends() {
        return trends;
    }
    public void setTrends(List<TwitterTrend> trends) {
        this.trends = trends;
    }
   
}

Step 4. create the main activity 
MainActivity.java


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runJSONParser();
}



public InputStream getJSONData(String url){
        DefaultHttpClient httpClient = new DefaultHttpClient();
        URI uri;
        InputStream data = null;
        try {
            uri = new URI(url);
            HttpGet method = new HttpGet(uri);
            HttpResponse response = httpClient.execute(method);
            data = response.getEntity().getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return data;
    }

public void runJSONParser(){
        try{
        Log.i("MY INFO", "Json Parser started..");
        Gson gson = new Gson();
        Reader r = new InputStreamReader(getJSONData("http://10.0.2.2/gsonTest/index.php"));
        Log.e("MY INFO", r.toString());
        TwitterTrends objs = gson.fromJson(r, TwitterTrends.class);
        Log.e("MY INFO", ""+objs.getTrends().size());
        for(TwitterTrend tr : objs.getTrends()){
            Log.e("TRENDS", tr.name + " - " + tr.url);
        }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

}

step 5.
Add the following lines into the xml file
 <uses-permission android:name="android.permission.INTERNET" /> 

Step 6.
run the app , u will see the following log


05-15 22:01:54.623: I/MY INFO(1796): Json Parser started..
05-15 22:01:54.733: E/MY INFO(1796): java.io.InputStreamReader@43e55058
05-15 22:01:54.773: E/MY INFO(1796): 10
05-15 22:01:54.773: E/TRENDS(1796): #nowplaying - http://search.twitter.com/search?q=%23nowplaying
05-15 22:01:54.773: E/TRENDS(1796): #nothingworsethan - http://search.twitter.com/search?q=%23nothingworsethan
05-15 22:01:54.773: E/TRENDS(1796): Dubai Mall - http://search.twitter.com/search?q=%22Dubai+Mall%22
05-15 22:01:54.773: E/TRENDS(1796): iPad Gets - http://search.twitter.com/search?q=%22iPad+Gets%22
05-15 22:01:54.773: E/TRENDS(1796): #SuperJuniorTrot - http://search.twitter.com/search?q=%23SuperJuniorTrot
05-15 22:01:54.773: E/TRENDS(1796): Justin Bieber - http://search.twitter.com/search?q=%22Justin+Bieber%22
05-15 22:01:54.773: E/TRENDS(1796): Click - http://search.twitter.com/search?q=Click
05-15 22:01:54.773: E/TRENDS(1796): Jaebum - http://search.twitter.com/search?q=Jaebum
05-15 22:01:54.773: E/TRENDS(1796): #tosavemoney - http://search.twitter.com/search?q=%23tosavemoney
05-15 22:01:54.773: E/TRENDS(1796): Protection - http://search.twitter.com/search?q=Protection

http://www.softwarepassion.com/android-series-parsing-json-data-with-gson/

2 comments: