The service is a proxy for your interface • The proxy dispatches method invocations to an InvocationHandler • The InvocationHandler uses reflection to create and cache a RestMethodInfo for each method • The RestMethodInfo is used to build a Request • The Client executes the request • The Converter deserializes the response body and returns an object of the correct type.
to every request • Great for authentication tokens or metadata like app version, platform, accept language, etc. • Interceptors in 1.0 are limited and can’t access the request body. • Example: add authentication token header restAdapter.setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("auth-token", "supersecret"); } })
make requests • Client is responsible for caching and cookie mgmt • Use OkHttp - you won’t have a choice later! • OkHttp interceptors can overwrite headers
Wait for multiple requests to complete before executing a callback • Only requires one error handler when composing requests or stream operations • Easily switch between threads depending on circumstance • Control timing of response callbacks when testing
1.x. • Jackson: Larger footprint, better performance for some use cases • Moshi: Unreleased, will be the default converter, leverages Okio The Client streams bytes to the Converter as they arrive so converter typically won’t be your choke point. CONVERTER CHOOSING A
fields with public getters and setters when applicable • Only use constructors when necessary • Transient fields will not be serialized • Use @NonNull and @Nullable everywhere (add support- annotations dependency) public class UserProfile { int id; @Nullable String name; @NonNull List<UserProfile> friends; public int getId() { return id; } @Nullable public String getName() { return name; } @NonNull public List<UserProfile> getFriends() { return friends; } }
(not your Retrofit implementation) • Define a class that implements service interface • Simulate network latency, variance, and error rate • Does not test your Converter or Client
its ability to seamlessly synchronize all test operations with the application under test. By default, Espresso waits for UI events in the current message queue to process and default AsyncTasks to complete before it moves on to the next test operation.”