After diving deep into the art of using Retrofit for network requests, we'll look in this post at the debugging capabilities of Retrofit. Let's review the covered topics in our Retrofit series.
Retrofit Series Overview
- Getting Started and Creating an Android Client
- Basics of API Description
- Creating a Sustainable Android Client
- URL Handling, Resolution and Parsing
- How to Change API Base Url at Runtime
- Multiple Server Environments (Develop, Staging, Production) (Coming soon)
- Share OkHttp Client and Converters between Retrofit Instances (Coming soon)
- Upgrade Guide from 1.9
- How to use OkHttp 3 with Retrofit 1
- Synchronous and Asynchronous Requests
- Send Objects in Request Body
- Add Custom Request Header
- Manage Request Headers in OkHttp Interceptor
- Dynamic Request Headers with @HeaderMap
- Multiple Query Parameters of Same Name
- Optional Query Parameters
- Send Data Form-Urlencoded
- Send Data Form-Urlencoded Using FieldMap
- How to Add Query Parameters to Every Request
- Add Multiple Query Parameter With QueryMap
- How to Use Dynamic Urls for Requests
- Constant, Default and Logic Values for POST and PUT Requests
- Cancel Requests
- Reuse and Analyze Requests
- Optional Path Parameters
- How to Send Plain Text Request Body
- Customize Network Timeouts
- How to Trust Unsafe SSL certificates (Self-signed, Expired)
- Dynamic Endpoint-Dependent Interceptor Actions
- How to Update Objects on the Server (PUT vs. PATCH)
- How to Delete Objects on the Server
- Introduction to (Multiple) Converters
- Adding & Customizing the Gson Converter
- Implementing Custom Converters
- How to Integrate XML Converter
- Access Mapped Objects and Raw Response Payload
- Supporting JSON and XML Responses Concurrently
- Handling of Empty Server Responses with Custom Converter
- Send JSON Requests and Receive XML Responses (or vice versa)
- Unwrapping Envelope Responses with Custom Converter
- Wrapping Requests in Envelope with Custom Converter
- Define a Custom Response Converter
- Introduction to Call Adapters
- Custom Call Adapter to Separate OnResponse Callback
- How to Integrate RxJava 1.x Call Adapter
- How to Integrate RxJava 2.x Call Adapter (Coming soon)
- How to Integrate Guava Call Adapter (Coming soon)
- Custom Call Adapter to Separate Network and Gson Errors (Coming soon)
- Callbacks (Coming soon)
- Annotations (Coming soon)
- Fluent Interface with Builders (Coming soon)
Logging in Retrofit 2
We’ve published a blog post on how to debug requests using Retrofit 2. If you already jumped on the second major version of Retrofit, please follow the link to the related post.
As you can see by our long list of published posts, we at Future Studio are fans of Retrofit. Just a few days ago we utilized another great feature of Retrofit: the request logging.
If you're having a similar problem and trouble understanding why a request from your Android app does not work the way it is intended, follow this guide for easy trouble shooting (or, at the very least, to be able to blame the API devs).
Activating Retrofit Logging
That we can view the requests and responses can be a nice assistant. However, it's deactivated by default. Luckily, enabling the logging feature is fast and easy:
RestAdapter.Builder builder = new RestAdapter.Builder() .setEndpoint(API_LOCATION) .setLogLevel(RestAdapter.LogLevel.FULL) // this is the important line .setClient(new OkClient(new OkHttpClient()));
This is all you've to do. Now start the app or the tests and force an execution of the requests in question.
Using the Logs
After the requests were made, take a look at the Android Logcat of your testing device. Retrofit posts a bunch of interesting things:
As you can see, this includes the entire request and response body. While this can be useful and necessary, the information can be too much and clutter up your log.
Knowing the Levels
Not to worry, Retrofit has different logging levels to match the amount of required information without blowing up your logs too much. Let's take a look at the various levels:
Description: No logging.
Description: Log only the request method and URL and the response status code and execution time.
Description: Log the basic information along with request and response headers.
Description: Log the basic information along with request and response objects via toString().
Description: Log the headers, body, and metadata for both requests and responses.
see above at Using the Logs
We hope this post gave you a short introduction into a feature of Retrofit, which can be tremendously helpful. Keep it in your mind if you want to debug a specific request or just log all the networking your app is doing.
If you've questions, please let us know at @futurestud_io.