Android Open Source - demo-flickr-feed-android Mock Rest Client






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;
/*www .ja v  a  2s  .c o m*/
import android.content.Context;
import android.net.Uri;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import retrofit.RestAdapter;
import retrofit.client.Client;
import retrofit.client.Header;
import retrofit.client.Request;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;
import uk.org.tomek.flickrfeed.rest.service.FlickrPostsService;
import uk.org.tomek.flickrfeed.utils.Logger;
import uk.org.tomek.flickrfeed.utils.StringUtils;

/**
 * Mock rest client.
 *
 * Created by tomek on 01/11/14.
 */
public class MockRestClient implements RestClient {

    private static final String BASE_MOCK_URL = "http://www.mockme.com/";
    private final Logger mLogger = Logger.getLogger(MockRestClient.class);
    private final RestAdapter mRestAdapter;
    private final Context mContext;
    private FlickrPostsService mPostsService;

    public MockRestClient(Context context) {
        mContext = context;
        mLogger.v("Creating MockRestClient");
        Gson gson = new GsonBuilder()
                .create();

        mRestAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.BASIC)
                .setClient(new MockHttpClient())
                .setEndpoint(BASE_MOCK_URL)
                .setConverter(new FlickrConverter(gson))
                .build();
    }


    @Override
    public FlickrPostsService getFlickrService() {
        if (mPostsService == null) {
            mPostsService = mRestAdapter.create(FlickrPostsService.class);
        }
        return mPostsService;
    }

    /**
     * Mock HTTP client for mocked requests.
     */
    private class MockHttpClient implements Client {
        @Override
        public Response execute(Request request) throws IOException {

            Uri uri = Uri.parse(request.getUrl());
            mLogger.v("Mock Request :%s", uri.getPath());

            String responseStr = "";
            if (uri.getPath().contains("/feeds/photos_public")) {
                responseStr = StringUtils.loadJSONFromAsset(mContext, "mock_feed.json");
            }

            List<Header> emptyHeaders = Collections.emptyList();
            return new Response(request.getUrl(), 200, "nothing", emptyHeaders,
                    new TypedByteArray("application/json", responseStr.getBytes()));
        }
    }
}




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