Android Open Source - MyFlickrSearch Helpers






From Project

Back to project page MyFlickrSearch.

License

The source code is released under:

MIT License

If you think the Android project MyFlickrSearch 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.devng.flickrsearch.common;
/*  w w w . j  a  v a  2  s . c  o  m*/
import android.net.http.AndroidHttpClient;
import android.util.Log;

import com.devng.flickrsearch.model.FlickrImgRef;
import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * A number of static functions which make my life easier.
 */
public class Helpers {

  private static final String LOG_TAG = Helpers.class.getSimpleName();

  private Helpers() {
  }

  public static byte[] doGet(final String url) {
    Log.d(LOG_TAG, "Requesting " + url);
    AndroidHttpClient client = AndroidHttpClient.newInstance(Config.HTTP_USER_AGENT);
    try {
      HttpGet getRequest = new HttpGet(url);
      HttpResponse response = client.execute(getRequest);
      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        Log.e(LOG_TAG, "Got a non OK ("
            + response.getStatusLine().getStatusCode()
            + ")response for " + url);
        return null;
      }
      return EntityUtils.toByteArray(response.getEntity());
    } catch (IOException e) {
      Log.e(LOG_TAG, "Could not open " + url, e);
    } finally {
      client.close();
    }
    return null;
  }

  public static <T> T fromJson(final String jsonString, final Class<T> type) {
    Gson gson = new Gson();
    return gson.fromJson(jsonString, type);
  }


  public static String encodeUrlString(String input) {
    try {
      return URLEncoder.encode(input, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      Log.e(LOG_TAG, "Unsupported Encoding");
      return null;
    }
  }

  /**
   * helper method, to construct the url from the json object.
   */
  public static String constructFlickrImgUrl(final FlickrImgRef ref, final Enums.ImageSize imageSize) {
    StringBuilder sb = new StringBuilder();

    sb.append("http://farm");
    sb.append(ref.getFarm());
    sb.append(".static.flickr.com/");
    sb.append(ref.getServer());
    sb.append("/");
    sb.append(ref.getId());
    sb.append("_");
    sb.append(ref.getSecret());
    sb.append(imageSize.getSuffix());
    sb.append(".jpg");

    return sb.toString();
  }
}




Java Source Code List

com.devng.flickrsearch.AbstractBackButtonActivity.java
com.devng.flickrsearch.ImageViewerActivity.java
com.devng.flickrsearch.MainActivity.java
com.devng.flickrsearch.MainModule.java
com.devng.flickrsearch.SearchResultsActivity.java
com.devng.flickrsearch.SearchResultsListAdapter.java
com.devng.flickrsearch.common.Config.java
com.devng.flickrsearch.common.Enums.java
com.devng.flickrsearch.common.Helpers.java
com.devng.flickrsearch.model.FlickrImgContainer.java
com.devng.flickrsearch.model.FlickrImgRef.java
com.devng.flickrsearch.model.FlickrImgRoot.java
com.devng.flickrsearch.task.DownloadFlickrImagesHandler.java
com.devng.flickrsearch.task.DownloadFlickrImagesTask.java
com.devng.flickrsearch.task.SearchFlickrResultHadler.java
com.devng.flickrsearch.task.SearchFlickrTask.java