0

i know i have posted a lot of questions on the same topic but i keep coming across issues i don't really know how to solve. thank you to everyone that has helped me i rally do appreciate it. i think i now know the error some i'm going to post everything clearly for you. i am trying to parse json from a Url and populate a list so heres the json feed in this case i just want the title

{"code":200,"error":null,"data":{"news":[{"news_id":"8086","title":"Tickets for Player of the Year award on general sale","tagline":"Event to be held Sunday 25th March at Holiday Inn, Barnsley","content":"Tickets for the inaugural Evo-Stik NPL Player of the Year event are now on sale to the general public.\r\n\r\nPriced at \u00a335, tickets include a three course meal, plus entertainment from renowned tenor Martin Toal and guest speaker Fred Eyre.\r\n\r\nAwards at the event include the Player and Young Player of the Year for each division, as well as divisional teams of the year, the Fans\u2019 Player of the Year and a League Merit award.\r\n\r\nTo purchase your ticket, send a cheque for \u00a335 payable to \u201cNorthern Premier League\u201d to Alan Ogley, 21 Ibberson Avenue, Mapplewell, Barnsley, S75 6BJ. Please include a return address for the tickets to be sent to, and the names of those attending. \r\n\r\nFor more information, e-mail Tom Snee or contact Event organiser Alan Ogley on 07747 576 415\r\n\r\nNote: Clubs can still order tickets by e-mailing Angie Firth - simply state how many tickets you require and you will be invoiced accordingly.\r\n\r\nAddress of venue: Barnsley Road, Dodworth, Barnsley S75 3JT (just off Junction 37 of M1)","created":"2012-02-29 12:00:00","category_id":"1","img":"4539337","category":"General","fullname":"Tom Snee","user_id":"170458","image_user_id":"170458","image_file":"1330519210_0.jpg","image_width":"600","image_height":"848","sticky":"1","tagged_division_id":null,"tagged_team_id":null,"tagged_division":null,"tagged_team":null,"full_image":"http:\/\/images.pitchero.com\/up\/league-news-default-full.png","image_primary":"http:\/\/images.pitchero.com\/ui\/170458\/lnl_1330519210_0.jpg","image_secondary":"http:\/\/images.pitchero.com\/ui\/170458\/lns_1330519210_0.jpg","image_original":"http:\/\/images.pitchero.com\/ui\/170458\/1330519210_0.jpg","image_small":"http:\/\/images.pitchero.com\/ui\/170458\/sm_1330519210_0.jpg"}

heres my android code

 BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();





            // Instantiate a JSON object from the request response
            JSONObject obj = new JSONObject(json);
            List<String> items = new ArrayList<String>();
            Log.i("json", "getting json");

            JSONArray jArray = obj.getJSONArray("news");


            for (int i=0; i < jArray.length(); i++)
            {    JSONObject oneObject = jArray.getJSONObject(i);
                items.add(oneObject.getString("title"));

            }

            setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
            ListView list = getListView();
            list.setTextFilterEnabled(true);


            list.setOnItemClickListener(new OnItemClickListener(){

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
                }



            });


        } catch(Exception e){
            // In your production code handle any errors and catch the individual exceptions
            e.printStackTrace();
        }

       }

I have tried logging it at LogCat gets the result no value for news.

so my question is why?

iamlukeyb
  • 6,487
  • 12
  • 29
  • 40

1 Answers1

0

First of your json response is invalid.

You can test your JSON response at JSONLint.com

Second, first you have to fetch "data" object as a JSONObject and then you can able to fetch "news" JSONArray.

JSONObject objData = obj.getJSONObject("data");
JSONArray jArray = objData.getJSONArray("news");
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 1
    hi it is valid i couldn't post the whole thing because it takes me over the 30000 limit. the json provide is just one news story i just posted it so you could see what i was trying to extract from it. but thanks for the link very useful tool – iamlukeyb Mar 11 '12 at 08:04
  • Paresh it worked i can't thank you enough my friend. I've been tried to fix that for 2 weeks. it was that i wasn't loading in data before i could extract the news. you sir are a legend. – iamlukeyb Mar 11 '12 at 08:09
  • Welcome dear...even I would suggest you an example you should go through to gain enough confidence for implementing [JSON Parsing](http://www.technotalkative.com/android-json-parsing/). – Paresh Mayani Mar 11 '12 at 08:12
  • one thing how to a send the information from one activity to another for example i click on one of the titles it takes you a page and loads the information in for exile image, title and content – iamlukeyb Mar 11 '12 at 08:52
  • You can use `Intent` for that..Just create an ArrayList , in your case Object is News and pass a particular object whenever a particular item is clicked from that ListView. check [this example](http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/) – Paresh Mayani Mar 11 '12 at 08:57
  • thanks Paresh I'm so happy now programming can be stressful but so rewarding when it comes together – iamlukeyb Mar 11 '12 at 09:05
  • sorry to pester you again i have created a relative layout for the single_item so it now contains an image how do i feed the image into that from the json is that done in my list adapter – iamlukeyb Mar 11 '12 at 09:09
  • hi Paresh heres the new question http://stackoverflow.com/questions/9653854/android-feeding-images-from-a-json-feed-into-a-list – iamlukeyb Mar 11 '12 at 09:38