Android Open Source - demo-flickr-feed-android Flickr Converter






From Project

Back to project page demo-flickr-feed-android.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project demo-flickr-feed-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 uk.org.tomek.flickrfeed.rest;
// w w  w . j a va2 s.  c  o m
import com.google.gson.Gson;

import org.apache.http.protocol.HTTP;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

import retrofit.converter.ConversionException;
import retrofit.converter.GsonConverter;
import retrofit.mime.TypedInput;
import uk.org.tomek.flickrfeed.utils.Logger;

/**
 * Custom GSON converter used to remove extra information from the input JSON feed.
 *
 * Created by tomek on 23/11/14.
 */
public class FlickrConverter extends GsonConverter {

    private final Logger mLogger = Logger.getLogger(FlickrConverter.class);

    public FlickrConverter(Gson gson) {
        super(gson);
    }

    public FlickrConverter(Gson gson, String charset) {
        super(gson, charset);
    }

    @Override
    public Object fromBody(TypedInput body, Type type) throws ConversionException {
        String string = RestUtils.typedInputToString(body);
        String replaced = string.replace("jsonFlickrFeed(", "");
        // remove last character (closing bracket)
        String substring = replaced.substring(0, replaced.length() - 1);
        body = new JsonTypedInput(substring.getBytes(Charset.forName(HTTP.UTF_8)));

        return super.fromBody(body, type);
    }

    /**
     *
     */
    private static class JsonTypedInput implements TypedInput {

        private final byte[] mReplaced;

        public JsonTypedInput(byte[] replaced) {
            mReplaced = replaced;
        }

        @Override
        public String mimeType() {
            return "application/json; charset=UTF-8";
        }

        @Override
        public long length() {
            return mReplaced.length;
        }

        @Override
        public InputStream in() throws IOException {
            return new ByteArrayInputStream(mReplaced);
        }
    }

}




Java Source Code List

tomek.co.uk.flickrfeed.ApplicationTest.java
uk.org.tomek.flickrfeed.App.java
uk.org.tomek.flickrfeed.activity.ImageDetailsActivity.java
uk.org.tomek.flickrfeed.activity.MainActivity.java
uk.org.tomek.flickrfeed.activity.SettingsActivity.java
uk.org.tomek.flickrfeed.adapter.FeedAdapter.java
uk.org.tomek.flickrfeed.fagment.SettingsFragment.java
uk.org.tomek.flickrfeed.fagment.StartingFragment.java
uk.org.tomek.flickrfeed.model.Feed.java
uk.org.tomek.flickrfeed.model.Item.java
uk.org.tomek.flickrfeed.model.Media.java
uk.org.tomek.flickrfeed.rest.FlickrConverter.java
uk.org.tomek.flickrfeed.rest.MockRestClient.java
uk.org.tomek.flickrfeed.rest.RealRestClient.java
uk.org.tomek.flickrfeed.rest.RestClient.java
uk.org.tomek.flickrfeed.rest.RestUtils.java
uk.org.tomek.flickrfeed.rest.service.FlickrPostsService.java
uk.org.tomek.flickrfeed.utils.Logger.java
uk.org.tomek.flickrfeed.utils.StringUtils.java