Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a URL for a twitter feed and I have created a string from it and I want to put it in a JSONArray so I can call back the individual items. In this case - "text".

Here's the json

Here's my code:

try {
   // Create a new HTTP Client
   DefaultHttpClient defaultClient = new DefaultHttpClient();
   // Setup the get request
   HttpGet httpGetRequest = new HttpGet(
         "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");

   // 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 obj = new JSONObject(json);
   List<String> items = new ArrayList<String>();

   JSONArray jArray = obj.getJSONArray(json);

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

   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();
}
share|improve this question
What's the problem? – Binyamin Sharet Mar 11 '12 at 14:05
its not displaying anything in my list view I'm getting a JSONeexception in my LogCat. – iamlukeyb Mar 11 '12 at 14:15

1 Answer

up vote 1 down vote accepted

You are trying to parse the response as JSONObject ({}), while it is a JSONArray ([]). You should probably remove the line:

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

instead, you need:

JSONArray jArray = new JSONArray(json);
share|improve this answer
ok I've edited it and now just getting the red cross it doesn't like json.getString String json = reader.readLine(); Log.v(json,"jsonfeed"); List<String> items = new ArrayList<String>(); for (int i=0; i < json.length(); i++) { items.add(json.getString("text")); Log.i("items", "items"); } setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items)); – iamlukeyb Mar 11 '12 at 14:26
ok figured it out turns out you was right but i also needed to convert into a JSONArray JSONArray jArray = new JSONArray(json); – iamlukeyb Mar 11 '12 at 14:29
my mistake, see my fix. – Binyamin Sharet Mar 11 '12 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.