allow the user to pull down on their timeline and see new tweets? Pull to Refresh Users can sign in using their existing Twitter accounts and see content unique to their account OAuth Users can view tweets from their home timeline. How do we make authenticated API requests to get this data? Networking twitter: user stories
the button used to start OAuth flow // Uses the client to initiate OAuth authorization // This should be tied to a button used to login public void loginToRest(View view) { getClient().connect(); }
@Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { // Parse JSON response into a list of Tweet objects you can use in your RecyclerView List<Tweet> tweets = Tweet.fromJsonMultiple(response); callback.onTweetsReceived(tweets); } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { // Figure out why the request failed - you can log, print error messages, etc. callback.onTweetsReceivedError(); } }; )
In the RecyclerView.Adapter class // Clear the data public void clear() { items.clear(); notifyDataSetChanged(); } // Add a list of updated items public void addAll(List<Tweet> list) { items.addAll(list); notifyDataSetChanged(); }
class TimelineActivity extends Activity { private SwipeRefreshLayout swipeContainer; @Override protected void onCreate(Bundle savedInstanceState) { //... swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer); // Setup refresh listener which triggers new data loading swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // Method to refresh the list fetchTimelineAsync(1); } }); } public void fetchTimelineAsync(int sinceId) { // Send the network request to fetch the updated data client.getHomeTimeline(sinceId, new JsonHttpResponseHandler() { public void onSuccess(JSONArray json) { adapter.clear(); adapter.addAll(Tweet.fromJsonMultiple(json)); // Remember to call setRefreshing(false) to signal refresh has finished swipeContainer.setRefreshing(false); } }); } }