Java API + Flickr API

I decided to play with Flickr API's for Java FX coding. But in between I found myself in hell and I started with Java :-). As all of you know Flickr Support hell lot of API to view Image, to search image, to search comment, Image translation, Image upload and many more. Check out here for detailed API. Now using these API's are not at all tough, because its all a game of XML.

Here I have written a small code, which do this :

1. It search one image(it can work for more than one image) from search API of Flickr.
2. It writes the search data on a XML, which I am copying at D:\\Hello1.xml.
3. And finally the code is using XML parsing techniques to get the information required for image view.
4. Then I use JDK6 feature of Desktop and open the default browser with the parsed URL. And
Congratulations, you can see the image.

Code, can look little big because of bad coding and writing lot of repetitive things :D.  

package flickrapp;

import java.awt.Desktop;
import javax.xml.namespace.QName;
import java.util.\*;

public class Main {

    public static void main(String args[]) throws Exception {
        URLConnection uc = new URL("").openConnection();
        DataInputStream dis = new DataInputStream(uc.getInputStream());
        FileWriter fw = new FileWriter(new File("D:\\\\Hello1.xml"));
        String nextline;
        String[] servers = new String[10];
        String[] ids = new String[10];
        String[] secrets = new String[10];
        while ((nextline = dis.readLine()) != null) {
        String filename = "D:\\\\Hello1.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")) {
                    Iterator iterator = element.getAttributes();

                    while (iterator.hasNext()) {

                        Attribute attribute = (Attribute);
                        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;
        String flickrurl = "" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg";
        try {
            URI uri = new URI(flickrurl);
            Desktop desktop = null;
            if (Desktop.isDesktopSupported()) {
                desktop = Desktop.getDesktop();

            if (desktop != null) {
        } catch (IOException ioe) {
        } catch (URISyntaxException use) {

Now see this line :

URLConnection uc = new URL("").openConnection();

Here some important thing to see :    -> this is the way to write Flickr API.

api_key=e3471e67d4ac10c64055420d9b211b4f  -> required for service. This is my api_key, you can have your own. Just go to flickr service and generate your API_key

per_page=1  -> Here is what I meant one image, if you can change this to 10. It will gather information of top 10 images in XML file.

text=Bangalore  -> Sorry, I have hard coded it for now. This is the search string.

Now, look at the XML file get generated in D:\\Hello1.xml. You can see one entry with tag photo inside photos. So, we need to take some data from this XML file and add in proper style to get the correct URL and that is here:

String flickrurl = "" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg";

Again, lot of things are hard coded(which I will correct in next post). Since only one image (i=0). I am assuming its a jpg image :D. Now calling Desktop API, you can load this image on default browser.

Now, this is still a live question, for certain keyword search, it gives the same result like when I search for keyword "Vaibhav", code and search box of Flickr provided the same result(which is not my photo :-( )  whereas if I search on things like "Bangalore", result is not similar for many cases. I don't know how Flickr handles it internally.

Probably next I will try to upload image or translate image but in Java FX :-)



Your code just got Groovier :)

import java.awt.Desktop

def flickrURL =

// flickr xml root
def rsp = new XmlSlurper().parseText(flickrURL.toURL().text)

// store all dah image urls here
def imageList = []
def flickrImageURLBase = ""

// reads just like the xml :) {
photo ->
imageList << flickrImageURLBase + "${photo.@server}/${photo.@id}_${photo.@secret}.jpg"

// images galore - just showing the first one here!

Posted by kodeninja on August 04, 2008 at 08:36 AM IST #

You want to say this much is the code i need to write in Groovy ! is it so ?

Posted by Vaibhav Choudhary on August 04, 2008 at 09:17 AM IST #

You betcha, buddy!

Download Groovy, if u haven't yet, and just run this code as is. Give it a spin and be enlightened :)


Posted by kodeninja on August 04, 2008 at 09:44 AM IST #

Ya that's why we like scripting language. Ya I know little of groovy and can understand this is a powerful language ! Lets see if java fx code can make it small :-)

Posted by Vaibhav on August 04, 2008 at 10:10 AM IST #

@kodeninja darn! I was going to do the same thing. Look how clean that groovy is :)

Posted by Robert on August 05, 2008 at 02:39 AM IST #

why not use flickrj?

Posted by Glenn Brown on August 05, 2008 at 03:36 AM IST #

oh my God but we all love Java :). So, let the code go long :D

Posted by Vaibhav Choudhary on August 05, 2008 at 05:53 AM IST #

iam expert in 2d& graphic designing

Posted by suhail on October 18, 2008 at 11:56 AM IST #

I need some help, please.
I can't import javax.xml.\* libraries. I don't touch Java and Eclipse IDE for a year. I suppose that I must add a .jar library of XML, right?
Can anyone guide me?

I'm a student from Spain, sorry for my english.

Posted by Kilik on June 27, 2009 at 11:16 AM IST #

Nice post using xml.
Here is another example using Java 5 and flickrj.

Posted by Steven on November 08, 2009 at 08:54 AM IST #

thanks you .thanks you .thanks you .thanks you .thanks you .

Posted by sikiş on November 23, 2009 at 10:55 AM IST #

Her turlu dizi ve sinema

Posted by dizinet on January 13, 2010 at 11:07 PM IST #

Good my friend - forget those idiots who try to invent new APIs jflickr this and that

and for those backrwards people like me who hate the word xml her's how with only strings

public void doIt(Vector v, int k, String srch) {
if(srch!=null) searchTerms=srch;
try {
URLConnection uc = new URL(""+
DataInputStream dis = new DataInputStream(uc.getInputStream());
FileWriter fw = new FileWriter(new File("flick1"));
String nextline, total="";
String[] servers = new String[10];
String[] ids = new String[10];
String[] secrets = new String[10];
while ((nextline = dis.readLine()) != null) {
System.out.println("line "+nextline);
int ind=0, i=0;
// StrinkTokenizer st=new StringTokenizer(total, "<photo");
while((ind=total.indexOf("<photo", ind+1))>-1) {
int idi=total.indexOf("id=", ind);
String id=total.substring(idi+4, total.indexOf('"', idi+4));
int sci=total.indexOf("secret=", ind);
String sc=total.substring(sci+8, total.indexOf('"', sci+8));
int svi=total.indexOf("server=", ind);
String sv=total.substring(svi+8, total.indexOf('"', svi+8));
String url=""+sv+"/"+id+"_"+sc+".jpg";
try {
BufferedImage URL(url));
if(v==null) paintOne(bi, 10+i\*5, 100+i);
else v.add(bi);
catch (IOException ex) { ex.printStackTrace(); return; }
// try { Thread.sleep(5000); } catch(InterruptedException ie) {}
if(i>k) break;
catch(Exception ex2) { ex2.printStackTrace(); return; }

Posted by guest on March 20, 2011 at 04:27 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed

Vaibhav Hi, I am Vaibhav Choudhary working in Oracle with JDK team. This blog is all about simple concept of Java, JVM and JavaFX.


« July 2015