Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android University - Week 3

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=47 Caren
December 07, 2018
41

Android University - Week 3

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=128

Caren

December 07, 2018
Tweet

Transcript

  1. Android University Week 3: Twitter

  2. Twitter : Part 1 - User can sign in to

    Twitter using OAuth login - User can view the tweets from their home timeline - User can refresh tweets timeline by pulling down to refresh
  3. OAuth Login

  4. OAuth Login Open Authorization

  5. OAuth Login Open Authorization “Token” based login

  6. OAuth Login Open Authorization “Token” based login Allows apps to

    share / access 
 information without the user
 having to give their credentials
  7. OAuth Login Open Authorization “Token” based login Allows apps to

    share / access 
 information without the user
 having to give their credentials Once apps have this ‘token’, they
 can use it to make API calls
  8. RestClientTemplate Makes it easy for us to login and make

    API calls with the token
  9. RestClientTemplate Makes it easy for us to login and make

    API calls with the token LoginActivity
  10. RestClientTemplate Makes it easy for us to login and make

    API calls with the token LoginActivity
  11. None
  12. Defining API Endpoints public void getHomeTimeline(int sinceId, AsyncHttpResponseHandler handler) {

    String apiUrl = getApiUrl("statuses/home_timeline.json"); RequestParams params = new RequestParams(); params.put(“since_id", 2); getClient().get(apiUrl, params, handler); } GET https://api.twitter.com/1.1/statuses/home_timeline.json&since_id=2
  13. Defining API Endpoints public void getHomeTimeline(int sinceId, AsyncHttpResponseHandler handler) {

    String apiUrl = getApiUrl("statuses/home_timeline.json"); RequestParams params = new RequestParams(); params.put(“since_id", 2); getClient().get(apiUrl, params, handler); } public void postTweet(String body, AsyncHttpResponseHandler handler) { String apiUrl = getApiUrl("statuses/update.json"); RequestParams params = new RequestParams(); getClient().post(apiUrl, params, handler); }
  14. Defining API Endpoints public void getHomeTimeline(int sinceId, AsyncHttpResponseHandler handler) {

    String apiUrl = getApiUrl("statuses/home_timeline.json"); RequestParams params = new RequestParams(); params.put(“since_id", 2); getClient().get(apiUrl, params, handler); } public void postTweet(String body, AsyncHttpResponseHandler handler) { String apiUrl = getApiUrl("statuses/update.json"); RequestParams params = new RequestParams(); getClient().post(apiUrl, params, handler); }
  15. Calling API Endpoints // SomeActivity.java RestClient client = RestApplication.getRestClient(this); client.getHomeTimeline(1,

    new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray json) { // Response is automatically parsed into a JSONArray // json.getJSONObject(0).getLong("id"); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } });
  16. Calling API Endpoints // SomeActivity.java RestClient client = RestApplication.getRestClient(this); client.getHomeTimeline(1,

    new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray json) { // Response is automatically parsed into a JSONArray // json.getJSONObject(0).getLong("id"); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } });
  17. Calling API Endpoints // SomeActivity.java RestClient client = RestApplication.getRestClient(this); client.getHomeTimeline(1,

    new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray json) { // Response is automatically parsed into a JSONArray // json.getJSONObject(0).getLong("id"); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } });
  18. Calling API Endpoints // SomeActivity.java RestClient client = RestApplication.getRestClient(this); client.getHomeTimeline(1,

    new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray json) { // Response is automatically parsed into a JSONArray // json.getJSONObject(0).getLong("id"); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } });
  19. public class Tweet { public String createdAt; public String body;

    public static Tweet fromJson(JSONObject jsonObject) throws JSONException { Tweet tweet = new Tweet(); tweet.createdAt = jsonObject.getString("created_at"); tweet.body = jsonObject.getString("text"); return tweet; } } Tweet Objects
  20. RecyclerView Refresher

  21. What we need to build a RecyclerView Data (to populate

    the views) Layout for each row in the RecyclerView An adapter to populate each row’s view with our data
  22. None
  23. 1. Set up data models, adapter, and RecyclerView


  24. 1. Set up data models, adapter, and RecyclerView
 a. Attach

    a data source for the adapter

  25. 1. Set up data models, adapter, and RecyclerView
 a. Attach

    a data source for the adapter
 public class UserListActivity extends AppCompatActivity { ArrayList<Contact> contacts; @Override protected void onCreate(Bundle savedInstanceState) { // ... // Lookup the recyclerview in activity layout RecyclerView rvContacts = (RecyclerView) findViewById(R.id.rvContacts); // Initialize contacts contacts = Contact.createContactsList(20); // Create adapter passing in the sample user data ContactsAdapter adapter = new ContactsAdapter(contacts); // Attach the adapter to the recyclerview to populate items rvContacts.setAdapter(adapter); // Set layout manager to position the items rvContacts.setLayoutManager(new LinearLayoutManager(this)); // That's all! } }
  26. 1. Set up data models, adapter, and RecyclerView
 a. Attach

    a data source for the adapter 2. Fetch data (from the Internet or local storage)
  27. 1. Set up data models, adapter, and RecyclerView
 a. Attach

    a data source for the adapter 2. Fetch data (from the Internet or local storage) 3. Translate fetched data into multiple Java objects that we can understand and use
  28. 1. Set up data models, adapter, and RecyclerView
 a. Attach

    a data source for the adapter 2. Fetch data (from the Internet or local storage) 3. Translate fetched data into multiple Java objects that we can understand and use 4. Add new objects to data source
  29. 1. Set up data models, adapter, and RecyclerView
 a. Attach

    a data source for the adapter 2. Fetch data (from the Internet or local storage) 3. Translate fetched data into multiple Java objects that we can understand and use 4. Add new objects to data source 5. Notify adapter that the data has been updated
  30. public void getTimeline() { RestClient client = RestApplication.getRestClient(this); // fetch

    data client.getHomeTimeline(1, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { for (int i = 0; i < response.length(); i++) { try { // translate data into java objects JSONObject jsonObjectTweet = response.getJSONObject(i); Tweet tweet = Tweet.fromJson(jsonObjectTweet); // add new objects to data source tweets.add(tweet); // notify adapter data has been updated adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } }); }
  31. public void getTimeline() { RestClient client = RestApplication.getRestClient(this); // fetch

    data client.getHomeTimeline(1, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { for (int i = 0; i < response.length(); i++) { try { // translate data into java objects JSONObject jsonObjectTweet = response.getJSONObject(i); Tweet tweet = Tweet.fromJson(jsonObjectTweet); // add new objects to data source tweets.add(tweet); // notify adapter data has been updated adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } }); }
  32. public void getTimeline() { RestClient client = RestApplication.getRestClient(this); // fetch

    data client.getHomeTimeline(1, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { for (int i = 0; i < response.length(); i++) { try { // translate data into java objects JSONObject jsonObjectTweet = response.getJSONObject(i); Tweet tweet = Tweet.fromJson(jsonObjectTweet); // add new objects to data source tweets.add(tweet); // notify adapter data has been updated adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } }); }
  33. List<Tweet> tweets = new ArrayList<>(); public void getTimeline() { RestClient

    client = RestApplication.getRestClient(this); // fetch data client.getHomeTimeline(1, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { for (int i = 0; i < response.length(); i++) { try { // translate data into java objects JSONObject jsonObjectTweet = response.getJSONObject(i); Tweet tweet = Tweet.fromJson(jsonObjectTweet); // add new objects to data source tweets.add(tweet); // notify adapter data has been updated adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } }); }
  34. public void getTimeline() { RestClient client = RestApplication.getRestClient(this); // fetch

    data client.getHomeTimeline(1, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { for (int i = 0; i < response.length(); i++) { try { // translate data into java objects JSONObject jsonObjectTweet = response.getJSONObject(i); Tweet tweet = Tweet.fromJson(jsonObjectTweet); // add new objects to data source tweets.add(tweet); // notify adapter data has been updated // this calls onBindViewHolder for the necessary items adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } }); }
  35. public void getTimeline() { RestClient client = RestApplication.getRestClient(this); // fetch

    data client.getHomeTimeline(1, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { for (int i = 0; i < response.length(); i++) { try { // translate data into java objects JSONObject jsonObjectTweet = response.getJSONObject(i); Tweet tweet = Tweet.fromJson(jsonObjectTweet); // add new objects to data source tweets.add(tweet); // notify adapter data has been updated adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // the network request failed for some reason. debug here } }); }