|
Distributed Computing This website demonstrates using wikis as teaching and learning tool. The course instructor is also happy to share the teaching materials here with those who find it readable. |
Tutorial /
Using Java to access Flickr Web ServicesIntroductionFlickr is a photo-sharing community that enables users to upload hundreds of photos and tag each photo with descriptive words. Other users can then search on these tags, enabling them to find and comment on the photos of other users. Flickr's active community and addictive sharing features have attracted millions of users. Even better, Flickr exposes a rich set of Web services that make the service highly hackable.
Go to the following websites to know more about Flickr service and Flickr API Sending a service request to FlickrString serviceEndpoint = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=24d52be90f046c1d2f05bbd2f47d40b6&per_page=5&text=apple"; URLConnection uc = new URL(serviceEndpoint).openConnection(); DataInputStream dis = new DataInputStream(uc.getInputStream()); FileWriter fileWriter = new FileWriter(new File("D:\\response.xml")); String nextline; String[] servers = new String[10]; String[] ids = new String[10]; String[] secrets = new String[10]; while ((nextline = dis.readLine()) != null) { fileWriter.append(nextline); } dis.close(); filewriter.close(); Parsing the XML file to get the image URLString filename = "D:\\response.xml"; XMLInputFactory factory = XMLInputFactory.newInstance(); System.out.println("FACTORY: " + factory); XMLEventReader r = factory.createXMLEventReader(filename, new FileInputStream(filename)); int i = -1; while (r.hasNext()) { XMLEvent event = r.nextEvent(); if (event.isStartElement()) { StartElement element = (StartElement) event; String elementName = element.getName().toString(); if (elementName.equals("photo")) { i++; Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); QName name = attribute.getName(); String value = attribute.getValue(); System.out.println("Attribute name/value: " + name + "/" + value); if ((name.toString()).equals("server")) { servers[i] = value; System.out.println("Server Value" + servers[0]); } if ((name.toString()).equals("id")) { ids[i] = value; } if ((name.toString()).equals("secret")) { secrets[i] = value; } } System.out.println(i); String flickrurl = "http://static.flickr.com/" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg"; System.out.println(flickrurl); } } } The XML API you may need to importimport javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; Your taskWrite a simple Java program to demonstrate the working of the code segments.
Write more code to enhance the features of your program.
Resources for you to probe further |