Android Open Source - Dreamer Flickr Request






From Project

Back to project page Dreamer.

License

The source code is released under:

Apache License

If you think the Android project Dreamer 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.android.fancyblurdemo.app;
//from  w ww. j  a  va2  s.  com
import com.android.fancyblurdemo.volley.NetworkResponse;
import com.android.fancyblurdemo.volley.ParseError;
import com.android.fancyblurdemo.volley.Response;
import com.android.fancyblurdemo.volley.toolbox.HttpHeaderParser;
import com.android.fancyblurdemo.volley.toolbox.JsonRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by kevin.marlow on 3/20/14.
 */
public class FlickrRequest extends JsonRequest<List<FlickrPhoto>> {

    private final String jsonReqsponseWrapper = "jsonFlickrApi(";
    private final boolean mIsHighRes;

    public FlickrRequest(String url, boolean isHighRes, Response.Listener<List<FlickrPhoto>> listener, Response.ErrorListener errorListener) {
        super(Method.GET, url, null, listener, errorListener);
        mIsHighRes = isHighRes;
    }

    @Override
    protected Response<List<FlickrPhoto>> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            String trimmedString = jsonString.substring(jsonReqsponseWrapper.length(), jsonString.length() - 1);
            JSONObject rootObj = new JSONObject(trimmedString);
            JSONObject photosObj = rootObj.getJSONObject("photos");
            JSONArray photosArray = photosObj.getJSONArray("photo");

            List<FlickrPhoto> photos = new ArrayList<FlickrPhoto>();

            for (int i = 0; i < photosArray.length(); i++) {
                JSONObject obj = photosArray.getJSONObject(i);
                FlickrPhoto photo = new FlickrPhoto();
                photo.id = obj.getString("id");
                photo.owner = obj.getString("owner");
                photo.secret = obj.getString("secret");
                photo.server = obj.getString("server");
                photo.farm = obj.getInt("farm");
                photo.title = obj.getString("title").trim().toUpperCase(Locale.US);
                photo.isPublic = obj.getInt("ispublic") != 0;
                photo.isFriend = obj.getInt("isfriend") != 0;
                photo.isFamily = obj.getInt("isfamily") != 0;
                String base = "http://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret;
                photo.photoUrl = base + "_z.jpg";
                photo.highResUrl = base + "_b.jpg";

                if (photo.title.contains("...")) {
                    photo.title = photo.title.replace("...", "... ");
                }
                photo.title = photo.title.replaceAll("[^\\x00-\\x7f]+", "").trim();

                String[] split = photo.title.split(" ");
                String newTitle = "";
                String longest = "";
                if (split.length != 0) {
                    int avgCharCount = Math.round(photo.title.length() / split.length);
                    if (avgCharCount > 14) {
                        avgCharCount /= 2;
                    }

                    for (String string : split) {
                        if (string.length() > ((mIsHighRes ? 3 : 2) * avgCharCount)) {
                            String part1 = string.substring(0, avgCharCount);
                            String part2 = string.substring(avgCharCount, string.length());
                            newTitle = newTitle + " " + part1 + "\n" + part2;
                            if (part1.length() > longest.length() || part2.length() > longest.length()) {
                                longest = (part1.length() > part2.length()) ? part1 : part2;
                            }
                        } else {
                            newTitle = newTitle + " " + string;
                            if (string.length() > longest.length()) {
                                longest = string;
                            }
                        }
                    }
                }

                int count = longest.length();
                longest = "";
                for (int j = count; j > 1; j--) {
                    longest += "W";
                }

                longest += "w";

                photo.title = newTitle.trim();
                photo.preferredWidthStr = longest;
                photos.add(photo);
            }

            return Response.success(photos, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}




Java Source Code List

com.android.fancyblurdemo.app.BitmapLruImageCache.java
com.android.fancyblurdemo.app.BlurImageView.java
com.android.fancyblurdemo.app.ContextWrapperEdgeEffect.java
com.android.fancyblurdemo.app.DreamAdapter.java
com.android.fancyblurdemo.app.EdgeEffectViewPager.java
com.android.fancyblurdemo.app.FlickrDream.java
com.android.fancyblurdemo.app.FlickrPhoto.java
com.android.fancyblurdemo.app.FlickrRequest.java
com.android.fancyblurdemo.app.FlickrScroller.java
com.android.fancyblurdemo.app.MainActivity.java
com.android.fancyblurdemo.app.MainApplication.java
com.android.fancyblurdemo.app.PhotoFragment.java
com.android.fancyblurdemo.app.PhotoSizeRequest.java
com.android.fancyblurdemo.app.PreCompositeTextTask.java
com.android.fancyblurdemo.app.RobotoTextView.java
com.android.fancyblurdemo.app.TopSwipeLayout.java
com.android.fancyblurdemo.app.VolleyManager.java
com.android.fancyblurdemo.app.imageblur.BlurDispatcher.java
com.android.fancyblurdemo.app.imageblur.BlurError.java
com.android.fancyblurdemo.app.imageblur.BlurManager.java
com.android.fancyblurdemo.app.imageblur.BlurQueue.java
com.android.fancyblurdemo.app.imageblur.BlurRequest.java
com.android.fancyblurdemo.app.imageblur.BlurResponseDelivery.java
com.android.fancyblurdemo.app.imageblur.BlurResponse.java
com.android.fancyblurdemo.app.imageblur.DiskLruImageCache.java
com.android.fancyblurdemo.app.imageblur.ImageBlurrer.java
com.android.fancyblurdemo.volley.AuthFailureError.java
com.android.fancyblurdemo.volley.CacheDispatcher.java
com.android.fancyblurdemo.volley.Cache.java
com.android.fancyblurdemo.volley.DefaultRetryPolicy.java
com.android.fancyblurdemo.volley.ExecutorDelivery.java
com.android.fancyblurdemo.volley.NetworkDispatcher.java
com.android.fancyblurdemo.volley.NetworkError.java
com.android.fancyblurdemo.volley.NetworkResponse.java
com.android.fancyblurdemo.volley.Network.java
com.android.fancyblurdemo.volley.NoConnectionError.java
com.android.fancyblurdemo.volley.ParseError.java
com.android.fancyblurdemo.volley.RequestQueue.java
com.android.fancyblurdemo.volley.Request.java
com.android.fancyblurdemo.volley.ResponseDelivery.java
com.android.fancyblurdemo.volley.Response.java
com.android.fancyblurdemo.volley.RetryPolicy.java
com.android.fancyblurdemo.volley.ServerError.java
com.android.fancyblurdemo.volley.TimeoutError.java
com.android.fancyblurdemo.volley.VolleyError.java
com.android.fancyblurdemo.volley.VolleyLog.java
com.android.fancyblurdemo.volley.toolbox.AndroidAuthenticator.java
com.android.fancyblurdemo.volley.toolbox.Authenticator.java
com.android.fancyblurdemo.volley.toolbox.BasicNetwork.java
com.android.fancyblurdemo.volley.toolbox.ByteArrayPool.java
com.android.fancyblurdemo.volley.toolbox.ClearCacheRequest.java
com.android.fancyblurdemo.volley.toolbox.DiskBasedCache.java
com.android.fancyblurdemo.volley.toolbox.HttpClientStack.java
com.android.fancyblurdemo.volley.toolbox.HttpHeaderParser.java
com.android.fancyblurdemo.volley.toolbox.HttpStack.java
com.android.fancyblurdemo.volley.toolbox.HurlStack.java
com.android.fancyblurdemo.volley.toolbox.ImageLoader.java
com.android.fancyblurdemo.volley.toolbox.ImageRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonArrayRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonObjectRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonRequest.java
com.android.fancyblurdemo.volley.toolbox.NetworkImageView.java
com.android.fancyblurdemo.volley.toolbox.NoCache.java
com.android.fancyblurdemo.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.fancyblurdemo.volley.toolbox.RequestFuture.java
com.android.fancyblurdemo.volley.toolbox.StringRequest.java
com.android.fancyblurdemo.volley.toolbox.Volley.java