Parsing complex JSON in Android

Guys i’m trying to parse this JSON below

{"response": 
{"message": "Success", 
"code": 2002, 
"payload": 
    {"article": [
                    {"timestamp_epoch": 1359967680, 
                    "search_score": 10.0, 
                    "headline": "When Sachin felt like bunking cricket practice", 
                    "timestamp": "2013-02-04 08:48:00", 
                    "related_article_count": 0, 
                    "excerpt": "...their campaign 'Support my School' by NDTV in Mumbai on Sunday. Sachin Tendulkar and Aishwarya Rai Bacchan during an event 3/5 Sachin Tendulkar and Aishwarya Rai Bacchan during an event India's Sachin Tendulkar with actress Aishwarya Rai Bachchan during the...", 
                    "source": {"name": "OneIndia", 
                                "url": "http://www.oneindia.in/", 
                                "daylife_url": "http://www.daylife.com/source/OneIndia", 
                                "rank": 3, 
                                "favicon_url": "http://favicon.daylife.com/imageserve/0e0Jd1Xczb7oW/favicon.png", 
                                "source_id": "0e0Jd1Xczb7oW", 
                                "type": "MAINSTREAM"}, 
                    "url": "http://feedproxy.google.com/~r/oneindia-cricket/~3/z1pS6w289SU/when-sachintendulkar-felt-like-bunking-cricket-practice-066016.html", 
                    "daylife_url": "http://www.daylife.com/article/07nC5eTbBE0YA", 

                    "article_id": "07nC5eTbBE0YA"}, 


                    {"timestamp_epoch": 1360361400, 
                    "search_score": 8.69279902947, 
                    "headline": "Sachin equals Sunny",
                           .
                           .
                           .
                           .
                           .

I’m just interested in headline and url values. I’m easily getting those values with this java code below.

JSONObject root = new JSONObject(result);
            JSONObject response = root.getJSONObject("response");

            String attributeId = response.getString("message");
            System.out.println(attributeId);

            JSONObject payload = response.getJSONObject("payload");
            JSONArray article = payload.getJSONArray("article");



            for (int i = 0; i < article.length(); i++) {
                JSONObject c = article.getJSONObject(i);
                String headline = c.getString("headline");
                String url = c.getString("url");
                System.out.println(url);
                System.out.println(headline);
                strings.add(c.getString("headline").toString());
            }
            arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, strings);

            resultList.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();

Here i’m setting headline in ListView(this part is working fine). And my question here is : when i click any list it should pass corresponding url to the WebView activity(actually I’m implementing this in another class). So how to achieve this, how to store corresponding url with headline?? please someone help me..

Thanks in advance..

Answer

You can create a class named Article having two properties(String heading and String url). Then generate getter/setter for both the properties. now create a ArrayList of Article type like..

ArrayList<Article> articleList = new ArrayList<Article>();

Then while parsing your data do as..

Article article = new article();

and put value into object like..

article.setHeading(c.getString("headline"));
article.setUrl(c.getString("url"));

and add object to arraylist like..

articleList.add(article);

after all when click any list item, to hadle it add OnItemClickListener in your main activity. it will give you position of item clicked. then you can get corresponding object’s url from arcleList like..

articleList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            String url = articleList.get(position).getUrl();
            Intent i = new Intent(MainActivity.this,   WebViewActivity.class);
            i.putExtra("url", url);
            startActivity(i);
        }
    });

now put this url into intent in main activity and receive it into webActivity and use it in webView finally…

Leave a Reply

Your email address will not be published. Required fields are marked *