Search code examples
androidjsontwitter-feed

json twitter search feed parsing in android


I have problem in parsing json from twitter search feed . for example the search url is:

https://search.twitter.com/search.json?q=android

Here is a link to the search

I want to get "result" array in json data. My code for fetch json and parse :

StringBuilder tweetFeedBuilder = new StringBuilder();
HttpClient tweetClient = new DefaultHttpClient();

//pass search URL string to fetch
HttpGet tweetGet = new HttpGet(searchURL);

//execute request
HttpResponse tweetResponse = tweetClient.execute(tweetGet);
//check status, only proceed if ok
StatusLine searchStatus = tweetResponse.getStatusLine();
if (searchStatus.getStatusCode() == 200) {
    //get the response
    HttpEntity tweetEntity = tweetResponse.getEntity();
    InputStream tweetContent = tweetEntity.getContent();
    //process the results
    InputStreamReader tweetInput = new InputStreamReader(tweetContent);
    BufferedReader tweetReader = new BufferedReader(tweetInput);

    while ((lineIn = tweetReader.readLine()) != null) 
    {
        tweetFeedBuilder.append(lineIn);
    }

    try{
        // A Simple JSONObject Creation
        JSONObject json=new JSONObject(tweetFeedBuilder);
        Log.i("Tweets","<jsonobject>\n"+json.toString()+"\n</jsonobject>");

        String str1 = "result";
        JSONArray jarray = json.getJSONArray(str1);

        for(int i = 0; i < jarray.length(); i++){

            JSONObject c = jarray.getJSONObject(i);
            String id = c.getString(TWEET_ID);
            String text = c.getString(TWEET_TEXT);
        }
    }
    catch(JSONException jexp){
        jexp.printStackTrace();
    }

After creating JSON object , JSONArray giving error in creating and goes in the catch block . Actually I want to fetch "result" array from JSON data.

But got error while creating. I just want to fetch user_id and text from the JSON data. I working in android platform and eclipse sdk.


Solution

  • As already mentioned in the comment, you are using the wrong key. It should be

    String str1 = "results"; // you are using result