Android Open Source - SimpleTwitterClient Timeline Activity

From Project

Back to project page SimpleTwitterClient.


The source code is released under:

Copyright (c) 2014 Keithen Hayenga Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the So...

If you think the Android project SimpleTwitterClient listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.codepath.apps.basictwitter;
/* w w  w  .j a  v  a  2 s .  com*/
import java.util.ArrayList;

import org.json.JSONArray;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.codepath.apps.basictwitter.models.Tweet;

// TimelineActivity is not the startup activity for the application.
// However, this is the main screen that the user is in, so this could 
// have been named MainActivity. It is from this view that we click the 
// compose action bar item, so this is where we return when the compose
// activity is finished. So we can send the tweet from here, just as we
// send the timeline requests from here.

public class TimelineActivity extends Activity {
  private TwitterClient client;
  private ArrayList<Tweet> tweets;
  private ArrayAdapter<Tweet> aTweets;
  private ListView lvTweets;

  protected void onCreate(Bundle savedInstanceState) {
    client = TwitterApplication.getRestClient();
    lvTweets = (ListView) findViewById(;
    tweets = new ArrayList<Tweet>();
    aTweets = new TweetArrayAdapter(this, tweets);
  public void populateTimeline() {
    client.getHomeTimeline(new JsonHttpResponseHandler() {
      public void onSuccess(JSONArray json) {
      public void onFailure(Throwable e, String s) {
        Log.d("debug", e.toString());
        Log.d("debug", s.toString());
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;
    private final int REQUEST_CODE = 73337;

    public void onComposeAction(MenuItem mi) {
    Intent i = new Intent(this, ComposeActivity.class);
    // No extras to put going in.
       startActivityForResult(i, REQUEST_CODE);
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // REQUEST_CODE is defined above
      if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        // Extract name value from result extras
        String stMessage = data.getExtras().getString("message");
        // Toast the name to display temporarily on screen
        //         Toast.makeText(this, stToUser, Toast.LENGTH_SHORT).show();

/////        // Before sending out our network request, make sure network is available.
/////        if (isNetworkAvailable() && isOnline()) {

          // Real task is to send out tweet:
          client.postTweet(new JsonHttpResponseHandler() {
            public void onSuccess(JSONArray json) {
              Log.d("debug", "Success sending Tweet!");
//              aTweets.addAll(Tweet.fromJSONArray(json));
            public void onFailure(Throwable e, String s) {
              Log.d("debug", e.toString());
              Log.d("debug", s.toString());
          }, stMessage);

/////        } else {
/////          // ToDo: put up dialog that we have no connectivity.
/////          Log.d("debug", "NETWORK NOT AVAILABLE!");
/////        }
    private Boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();

    public Boolean isOnline() {
        try {
            Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1");
            int returnVal = p1.waitFor();
            boolean reachable = (returnVal==0);
            return reachable;
        } catch (Exception e) {
        return false;


Java Source Code List