0

hi im expanded a previous question i asked just to make it clearer. i'm trying to get a json feed from a url and then create a list of titles using a list adapter. my json feed consists of a thing called data inside that an array of news and inside that thing called title.

when i run the app it force closes. I've been told its because i have a string inside a for loop and no instance of it outside to go in the adapter. i am new to this so any help would be appreciated heres my code

 try{
            // Create a new HTTP Client
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            // Setup the get request
            HttpGet httpGetRequest = new HttpGet("jsonfeed");

            // Execute the request in the client
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            // Grab the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();


            // Instantiate a JSON object from the request response
            JSONObject jsonObject = new JSONObject(json);



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

            String oneObjectsItem1 = null;
            for (int i=0; i < jArray.length(); i++)
            {
                JSONObject oneObject = jArray.getJSONObject(i);
                // Pulling items from the array

                String oneObjectsItem = oneObject.getString("title");



            }


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





        setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, oneObjectsItem)); 
        ListView list = getListView();
        list.setTextFilterEnabled(true);
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40
  • paste your json response string too – waqaslam Mar 08 '12 at 20:39
  • Hey looks like you're using my answer to your question here: http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android/9606629#9606629 , how about you accept it if it was helpful to you – bbedward Mar 08 '12 at 20:43
  • possible duplicate of [Android setlistAdapter error](http://stackoverflow.com/questions/9623565/android-setlistadapter-error) – user Mar 08 '12 at 20:44
  • {"code":200,"error":null,"data":{"news":[{"news_id":"8086","title":"Tickets for Player of the Year award on general sale"heres the json response – iamlukeyb Mar 08 '12 at 20:46

2 Answers2

0

you need to collect your string out from json into an ArrayList and later provide that list object to adapter. see the example below:

List<String> items = new ArrayList<String>();

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

  if(!TextUtils.isEmpty(oneObjectsItem))
    items.add(oneObjectsItem);
 }
}


//and set the list object to your adapter
setListAdapter(new ArrayAdapter<String>(this, R.layout.single_item, items);
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

Your error is not clear since we don't know what you're doing after you get the JSON, here's the proper way to get and handle it, and a possible fix for the string itself. Also, please consider accepting answers that you use, like in your question here: How to parse JSON in Android

Second of all, you're going over a whole array and creating a new object every time.

Remove This

String oneObjectsItem1 = null;
for (int i=0; i < jArray.length(); i++)
{
    JSONObject oneObject = jArray.getJSONObject(i);
    // Pulling items from the array
    String oneObjectsItem = oneObject.getString("title");
}

Use This

JSONArray jArray = MYJSONOBJECT.getJSONArray("JSONARRAYNAME");
ArrayList<String> myTitlesFromMyArray = new ArrayList<String>();
for (int i=0; i < jArray.length(); i++)
{
    JSONObject oneObject = jArray.getJSONObject(i);
    // TITLE IS IN MY ARRAY I WANT ALL TITLES IN MY LIST OF TITLES
    myTitlesFromMyArray.add(oneObject.getString("title"));
}

// Iterate through the ENTIRE list and do whatever you want with the items in order
for (String p: myTitlesFromMyArray)
{
     // Do something with p
}

Also, I think your stream for the json string may come back incomplete, to ensure it is complete change it

Remove This

String json = reader.readLine();

Use This

StringBuilder sb = new StringBuilder();

String input = null;
while ((input = reader.readLine()) != null)
{
    sb.append(input + "\n");
}
String json = input.toString();
Community
  • 1
  • 1
bbedward
  • 6,368
  • 7
  • 36
  • 59
  • hi thank you so much i will accept everything. this is good use getting an error here String titleFour = myTitlesFromMyArray.get(3);Type mismatch: cannot convert from CharSequence to String – iamlukeyb Mar 08 '12 at 21:06
  • edited my answer, it's entirely correct what you do with the ArrayList is up to you? – bbedward Mar 08 '12 at 21:09
  • thanks this helped how do i get string p into a list adapter – iamlukeyb Mar 08 '12 at 22:03
  • String newstitles = null; // Iterate through the ENTIRE list and do whatever you want with the items in order for (String p: myTitlesFromMyArray) { newstitles=p; } } catch(Exception e){ // In your production code handle any errors and catch the individual exceptions e.printStackTrace(); } setListAdapter(new ArrayAdapter(this, R.layout.single_item, newstitles})); ListView list = getListView(); list.setTextFilterEnabled(true);this is wht i've dne with it so far i still can't get it to work – iamlukeyb Mar 08 '12 at 22:08