Due: Monday May 8, 11pm
Crowdgrader Assignment: https://www.crowdgrader.org/crowdgrader/venues/view_venue/3127
In this homework, you have to write a news-reading app with two activities.
In the main activity, you have to do a GET request to the URL https://luca-ucsc-teaching-backend.appspot.com/hw4/get_news_sites
. You will receive back a JSON (I said, JSON) string that will look something like:
{"news_sites": [{"url": "http://www.latimes.com/", "subtitle": "News from Los Angeles", "title": "The Los Angeles Times"}, {"url": "http://www.lemonde.fr/", "subtitle": "A dash of France", "title": "Le Monde"}, {"url": "http://www.sierrasun.com/", "subtitle": "A taste of mountains", "title": "Sierra Sun \"North Lake Tahoe Bonanza\""}, {"url": "http://elpais.com/elpais/portada_america.html", "subtitle": "American version of Spanish paper", "title": "El Pais"}]}
As you see, what you are getting is a dictionary, where news_sites is mapped to a list of news sites. For each news site, I give you the title, subtitle, and URL. You need to produce a ListView, where for each valid result returned, you display the title and subtitle (follow the drawing included as example). Please take apart this information using the appropriate JsonObjectRequest and JSONObject methods. Notes:
You should get this list of news sites both when the app is opened, and when the user presses the refresh button.
When the user selects a news site by pressing on the list item (no need for a special button), you should go to a second Reader Activity. This reader activity should contain a WebView, which should be initialized to show the url given as part of the list.
Javascript should be enabled in the WebView.
These are the rules for WebView behavior:
http://www.latimes.com/
is a prefix of http://www.latimes.com/politics/la-pol-ca-maxine-waters-20170430-htmlstory.html
), you should load the link in the same WebView; otherwise you should let Android handle it with an external application.The WebViewExample contains examples of how to implement the above behaviors.
When you get a list of news sites, create a new list by removing all sites whose title or url is not known. Then use this "cleaned" list to drive the ListView display.
When a user presses an element of the list to go read that article, you need to start the ReaderActivity passing it the url to load. You can of course use AppInfo or other mechanisms, but the simplest is to add information to the intent used to start the activity.
You can do this via something like:
Intent i = new Intent(MainActivity.this, ReaderActivity.class);
i.putExtra("URL", url);
and on the receiving end, for instance in onStart:
String url;
Bundle extras = getIntent().getExtras();
if(extras == null) {
finish(); // No idea what else to do
} else {
url = extras.getString("URL");
}
(Warning: I have not tested this code, but if it doesn't work, you at least have a starting point for searching for the solution).
When you go back from ReaderActivity to the main activity, do a finish().
There is no starting code, but you should be able to put something together from AndroidHomePhone, ListViewExample, and WebViewExample. These contain the key examples you need.