Android Open Source - android Flickr Fetchr






From Project

Back to project page android.

License

The source code is released under:

MIT License

If you think the Android project android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.rdokollari.photogallery;
/*ww w .  ja v  a2s . com*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.net.Uri;
import android.util.Log;

/**
 * Handles the networking of this app.
 * 
 * @author Rizart Dokollari @ rdokollari.me
 * 
 */
public class FlickrFetchr {

  public static final String TAG = "FlickFetchr";

  private static final String ENDPOINT = "https://www.flickr.com/services/rest/";
  private static final String API_KEY = "d7232de1fb5d53739a09e168b02b765b";
  private static final String METHOD_GET_RECENT = "flickr.photos.getRecent";
  private static final String PARAM_EXTRAS = "extras";

  // tells Flickr to include the URL for the small version of the picture if
  // it is available.
  private static final String EXTRA_SMALL_URL = "url_s";

  // species the name of the photo XML element
  private static final String XML_PHOTO = "photo";

  /**
   * Fetches raw data from a URL and returns it as an array of bytes.
   */
  byte[] getURLBytes(String urlSpec) throws IOException {
    // creates a URL object form a string
    URL url = new URL(urlSpec);
    // create a connection object pointed at given URL, cast it to
    // HttpURLConnection type;
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    // repeatedly read until connection runs out of data
    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      InputStream in = connection.getInputStream();

      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        return null;
      } // end if

      int bytesRead = 0;
      byte[] buffer = new byte[1024];
      while ((bytesRead = in.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
      } // end while
        // close connection
      out.close();
      // spit out data
      return out.toByteArray();
    } finally {
      connection.disconnect();
    }

  } // end getURLBytes method

  /**
   * Converts the result form getURLBytes to a string
   */
  public String getURL(String urlSpec) throws IOException {
    return new String(getURLBytes(urlSpec));
  }

  /**
   * Builds an appropriate request URL and fetches its contents
   * 
   */
  public ArrayList<GalleryItem> fetchItems() {
    ArrayList<GalleryItem> items = new ArrayList<GalleryItem>();

    try {
      // use Uri.builder to build the complete URL for the Flickr API
      // request (properly escaped parameterized URLs)
      String url = Uri.parse(ENDPOINT).buildUpon()
          .appendQueryParameter("method", METHOD_GET_RECENT)
          .appendQueryParameter("api_key", API_KEY)
          .appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL)
          .build().toString();
      String xmlString = getURL(url);
      Log.i(TAG, "Received xml: " + xmlString);
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      XmlPullParser parser = factory.newPullParser();
      parser.setInput(new StringReader(xmlString));

      parseItems(items, parser);
    } catch (IOException ioe) {
      Log.e(TAG, "Failed to fetch items", ioe);
    } catch (XmlPullParserException xppe) {
      Log.e(TAG, "Failed to parse items", xppe);
    }

    return items;
  }

  /**
   * Uses XmlPullParser interface to identify each photo in the XML.
   * 
   * @param items
   * @param parser
   * @throws XmlPullParserException
   * @throws IOException
   */
  void parseItems(ArrayList<GalleryItem> items, XmlPullParser parser)
      throws XmlPullParserException, IOException {
    int eventType = parser.next();

    while (eventType != XmlPullParser.END_DOCUMENT) {
      if (eventType == XmlPullParser.START_TAG
          && XML_PHOTO.equals(parser.getName())) {
        String id = parser.getAttributeValue(null, "id");
        String caption = parser.getAttributeValue(null, "title");
        String smallUrl = parser.getAttributeValue(null,
            EXTRA_SMALL_URL);

        GalleryItem item = new GalleryItem(id, caption, smallUrl);
        items.add(item);
      } // end if

      // go to next vent of XML
      eventType = parser.next();
    } // end while
  } // end function parseItems
}




Java Source Code List

me.rdokollari.funququiz.QuoteActivity.java
me.rdokollari.funququiz.Quote.java
me.rdokollari.photogallery.FlickrFetchr.java
me.rdokollari.photogallery.GalleryItem.java
me.rdokollari.photogallery.PhotoGalleryActivity.java
me.rdokollari.photogallery.PhotoGalleryFragment.java
me.rdokollari.photogallery.SingleFragmentActivity.java