First create your Retrofit interface.
public interface LifeKitServerService { @GET("api/event") Observable<HttpResult<List<Event>>> getEventList(); }
Your requestor:
public final class HomeDataRequester { public static final String TAG = HomeDataRequester.class.getSimpleName(); public static final String SERVER_ADDRESS = BuildConfig.DATA_SERVER_ADDR + "/"; private LifeKitServerService mServerService; private HomeDataRequester() { OkHttpClient okHttpClient = new OkHttpClient.Builder()
If you use the second option (use the Retrofit interface for the Mock server data), you need MockRetrofit, use the following code:
public final class MockLifeKitServerService implements LifeKitServerService { public static final String TAG = MockLifeKitServerService.class.getSimpleName(); private BehaviorDelegate<LifeKitServerService> mDelegate; private Gson mGson = new Gson(); public MockLifeKitServerService(BehaviorDelegate<LifeKitServerService> delegate) { mDelegate = delegate; } @Override public Observable<HttpResult<List<Event>>> getEventList() { List<Event> eventList = MockDataGenerator.generateEventList(); HttpResult<List<Event>> httpResult = new HttpResult<>(); httpResult.setCode(200); httpResult.setData(eventList); LogUtil.json(TAG, mGson.toJson(httpResult)); String text = MockDataGenerator.getMockDataFromJsonFile("server/EventList.json"); if (TextUtils.isEmpty(text)) { text = mGson.toJson(httpResult); } LogUtil.d(TAG, "Text:\n" + text); text = mGson.toJson(httpResult); return mDelegate.returningResponse(text).getEventList(); }
4. My data is from the asset file (Asset / server / EventList.json), this file contains:
{ "code": 200, "data": [ { "uuid": "e4beb3c8-3468-11e6-a07d-005056a05722", "title": "title", "image": "http://image.jpg", "goal": 1500000, "current": 51233, "hot": true, "completed": false, "createdAt": "2016-06-15T04:00:00.000Z" } ] }
5. If you use the okhttp3 interceptor, you need a self-tuning interceptor, for example:
public final class MockHomeDataInterceptor implements Interceptor { public static final String TAG = MockHomeDataInterceptor.class.getSimpleName(); @Override public Response intercept(Chain chain) throws IOException { Response response = null; String path = chain.request().url().uri().getPath(); LogUtil.d(TAG, "intercept: path=" + path); response = interceptRequestWhenDebug(chain, path); if (null == response) { LogUtil.i(TAG, "intercept: null == response"); response = chain.proceed(chain.request()); } return response; } private Response interceptRequestWhenDebug(Chain chain, String path) { Response response = null; if (BuildConfig.DEBUG) { Request request = chain.request(); if (path.equalsIgnoreCase("/api/event")) {
6. Finally, you can request your server with the code:
mHomeDataRequester.getEventList(new Subscriber<HttpResult<List<Event>>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { LogUtil.e(TAG, "onError: ", e); if (mView != null) { mView.onEventListLoadFailed(); } } @Override public void onNext(HttpResult<List<Event>> httpResult) {
Thanks for reading.
Lai ZuLing Jul 29 '16 at 7:37 2016-07-29 07:37
source share