Back to project page demo-flickr-feed-android.
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.
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())); } } }