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

Android University - Week 3

Caren
December 07, 2018
49

Android University - Week 3

Caren

December 07, 2018
Tweet

Transcript

  1. 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
  2. OAuth Login Open Authorization “Token” based login Allows apps to

    share / access 
 information without the user
 having to give their credentials
  3. 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
  4. RestClientTemplate Makes it easy for us to login and make

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

    API calls with the token LoginActivity
  6. 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
  7. 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); }
  8. 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); }
  9. 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 } });
  10. 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 } });
  11. 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 } });
  12. 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 } });
  13. 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
  14. 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
  15. 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! } }
  16. 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)
  17. 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
  18. 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
  19. 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
  20. 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 } }); }
  21. 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 } }); }
  22. 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 } }); }
  23. 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 } }); }
  24. 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 } }); }
  25. 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 } }); }