Android Open Source - PhotoGallery Flickr Fetchr






From Project

Back to project page PhotoGallery.

License

The source code is released under:

MIT License

If you think the Android project PhotoGallery 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 com.ambergleam.photogallery;
// w ww.  j a  va2 s  . 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;

public class FlickrFetchr {

  public static final String TAG = "FlickrFetchr";

  public static final String PREF_SEARCH_QUERY = "searchQuery";
  public static final String PREF_LAST_RESULT_ID = "lastResultId";
  
  private static final String ENDPOINT = "http://api.flickr.com/services/rest/";
  private static final String API_KEY = "c9a3e695beb9b51c39ced23cbf8036d7";
  private static final String METHOD_GET_RECENT = "flickr.photos.getRecent";
  private static final String METHOD_SEARCH = "flickr.photos.search";
  private static final String PARAM_EXTRAS = "extras";
  private static final String PARAM_TEXT = "text";

  private static final String EXTRA_SMALL_URL = "url_s";

  private static final String XML_PHOTO = "photo";
  private static final String XML_PHOTOS = "photos";

  public ArrayList<GalleryItem> downloadGalleryItems(String url) {
    ArrayList<GalleryItem> items = new ArrayList<GalleryItem>();

    try {
      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 fetch items: ", xppe);
    }
    return items;
  }

  public ArrayList<GalleryItem> fetchItems() {
    String url = Uri.parse(ENDPOINT).buildUpon().appendQueryParameter("method", METHOD_GET_RECENT).appendQueryParameter("api_key", API_KEY).appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL).build().toString();
    return downloadGalleryItems(url);
  }

  public ArrayList<GalleryItem> search(String query) {
    String url = Uri.parse(ENDPOINT).buildUpon().appendQueryParameter("method", METHOD_SEARCH).appendQueryParameter("api_key", API_KEY).appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL).appendQueryParameter(PARAM_TEXT, query).build().toString();
    return downloadGalleryItems(url);
  }

  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();
        item.setId(id);
        item.setCaption(caption);
        item.setUrl(smallUrl);
        items.add(item);
      } else if (eventType == XmlPullParser.START_TAG && XML_PHOTOS.equals(parser.getName())) {
        String numPhotos = parser.getAttributeValue(null, "total");
        Log.i(TAG, "Total number of returned photos: " + numPhotos);
      }

      eventType = parser.next();
    }
  }

  byte[] getUrlBytes(String urlSpec) throws IOException {
    Log.i(TAG, "getUrlBytes");
    URL url = new URL(urlSpec);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      InputStream in = connection.getInputStream();

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

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

  public String getUrl(String urlSpec) throws IOException {
    return new String(getUrlBytes(urlSpec));
  }

}




Java Source Code List

com.ambergleam.photogallery.FlickrFetchr.java
com.ambergleam.photogallery.GalleryItem.java
com.ambergleam.photogallery.PhotoGalleryActivity.java
com.ambergleam.photogallery.PhotoGalleryFragment.java
com.ambergleam.photogallery.PollService.java
com.ambergleam.photogallery.SingleFragmentActivity.java
com.ambergleam.photogallery.ThumbnailDownloader.java