0

Hi I'm trying to retrieve public tweets from twitter but it seems like there is no explanation for it. I have the below code but it's not working. Any idea if this is an outdated code and if you have any sample examples that I could have a look at would be much appreciated.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new MyTask().execute();

}

private class MyTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog;

    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(MainActivity.this,
                "", "Loading. Please wait...", true);
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {

            HttpClient hc = new DefaultHttpClient();
            HttpGet get = new
                    HttpGet("http://search.twitter.com/search.json?q=android");

            HttpResponse rp = hc.execute(get);

            if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                String result = EntityUtils.toString(rp.getEntity());
                JSONObject root = new JSONObject(result);
                JSONArray sessions = root.getJSONArray("results");
                for (int i = 0; i < sessions.length(); i++) {
                    JSONObject session = sessions.getJSONObject(i);

                    Tweet tweet = new Tweet();
                    tweet.content = session.getString("text");
                    tweet.author = session.getString("from_user");
                    tweets.add(tweet);
                }
            }
        } catch (Exception e) {
            Log.e("TwitterFeedActivity", "Error loading JSON", e);
        }
        return null;

    }
    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
        setListAdapter(new TweetListAdaptor(
                MainActivity.this, R.layout.list_item, tweets));
    }

}

private class TweetListAdaptor extends ArrayAdapter<Tweet> {

    private ArrayList<Tweet> tweets;

    public TweetListAdaptor(Context context,

                            int textViewResourceId,
                            ArrayList<Tweet> items) {
        super(context, textViewResourceId, items);
        this.tweets = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        Tweet o = tweets.get(position);

        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        tt.setText(o.content);
        bt.setText(o.author);

        return v;
    }
}

}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sandra
  • 17
  • 5

1 Answers1

0

If you visit http://search.twitter.com/search.json?q=android in a Web browser, you will see:

{"errors":[{"message":"The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.","code":64}]}

Any idea if this is an outdated code

Yes, it is, as the above error message indicates.

You can read the Twitter REST API documentation, such as the documentation on their search API.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491