ITI0011:praktikum 7 T8

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

<source lang="java"> import org.json.JSONArray; import org.json.JSONObject;

public class JsonExample {

public static void main(String[] args) {

//args = new String[]{"mati", "kaal"};

System.out.println("args length:" + args.length); for (int i = 0; i < args.length; i++) { System.out.println("" + args[i]); }

JSONObject j = new JSONObject(getJson()); JSONArray results = j.getJSONArray("result"); for (int i = 0; i < results.length(); i++) { JSONObject obj = results.getJSONObject(i); String[] properties = JSONObject.getNames(obj); for (String code : properties) { JSONObject course = obj.getJSONObject(code); System.out.println("Course name: " + course.getString("name")); System.out.println("EAP: " + course.getInt("eap")); System.out.println("Lecture time: " + course.getString("lecture")); JSONArray practiceTimes = course.getJSONArray("practice"); for (int k = 0; k < practiceTimes.length(); k++) { System.out.println("Practice time: " + practiceTimes.get(k).toString()); } } }

String dateString = "14.10.2014"; SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); try { Date d = sdf.parse(dateString); System.out.println(d); Calendar c = Calendar.getInstance(); c.setTime(d); c.add(Calendar.DAY_OF_YEAR, 30); Date d2 = c.getTime(); System.out.println(d2); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

public static String getJson() { return "{'result':[{'ITI0011':{'name':'ITI0011', 'eap': 5, 'lecture':'T14', 'practice':['T8', 'N8', 'N14']}}]}"; }

}

</source>

Exercise

Write a program that reads argumends from command line. Every argument is a name. For every name, you should make a query to Wikipedia and retrieve some basic information.

For example, if you run the program with arguments "Tallinn" "Estonia" your program should call wikipedia two times: first with "Tallinn", then with "Estonia".

For getting information from Wikipedia, you can use http://dijkstra.cs.ttu.ee/~ago/iti0011/WikiAPI.jar library (a very simple library to just demonstrate how to use a library). This has a method getWikiInformation(String name) or getWikiInformation(String name, int format). Read javadoc for more. getWikiInformation just reads all the information from Wikiedpia API and returns it as one (huge) String.

Depending on the format you use, you should the the value of "touched" property (when the page is last accessed). If you want to use JSON-format, you can use this library: http://dijkstra.cs.ttu.ee/~ago/iti0011/json-20140107.jar

You should read out "touched" property value and represent it as a date. Try to add 44 days to that date and see, which date it is.

For date parsing, use SimpleDateFormat. To add days, use Calendar object.