| Start |
|---|
| A Simple GET Request We are going to be showing how to make a Add the dependencies for retrofit library as described in the Remarks section. Example of expected JSON object: Example of JSON array: Example of corresponding model class: The There's a lot going on here in a pretty compact space so let's break it down: Creating a wrapper class: Now we will make a little wrapper class for our API to keep the Retrofit initialization code wrapped up nicely. This class creates a GSON instance to be able to parse the JSON response, creates a Retrofit instance with our base url and a GSONConverter and then creates an instance of our API. Calling the API: This uses our API interface to create a Note: Parsing a JSON array of primitive objects (like String, Integer, Boolean, and Double) is similar to parsing a JSON array. However, you don't need your own model class. You can get the array of Strings for example by having the return type of the call as |
| Add logging to Retrofit2 Retrofit requests can be logged using an intercepter. There are several levels of detail available: NONE, BASIC, HEADERS, BODY. See Github project here (opens new window).
Exposing the logs in the Terminal(Android Monitor) is something that should be avoided in the release version as it may lead to unwanted exposing of critical information such as Auth Tokens etc. To avoid the logs being exposed in the run time, check the following condition For example: |
| Debugging with Stetho Add the following dependencies to your application. In your Application class' When creating your Then set this custom OkHttp instance in the Retrofit instance. Now connect your phone to your computer, launch the app, and type |
| A simple POST request with GSON Sample JSON: Define your request: Define your service (endpoints to hit): Define your singleton instance of the network client: Define a simple model object for the device: Define controller to handle the requests for the device |
| Upload multiple file using Retrofit as multipart Once you have setup the Retrofit environment in your project, you can use the following example that demonstrates how to upload multiple files using Retrofit: Following is the interface |
| Download a file from Server using Retrofit2 Interface declaration for downloading a file The option 1 is used for downloading a file from Server which is having fixed URL. and option 2 is used to pass a dynamic value as full URL to request call. This can be helpful when downloading files, which are dependent of parameters like user or time. Setup retrofit for making api calls Now, make implementation of api for downloading file from server And after getting response in the callback, code some standard IO for saving file to disk. Here is the code: Note we have specified ResponseBody as return type, otherwise Retrofit will try to parse and convert it, which doesn't make sense when you are downloading file. If you want more on Retrofit stuffs, got to this link as it is very useful. [1]: https://futurestud.io/blog/retrofit-getting-started-and-android-client (opens new window) |
| Retrofit with OkHttp interceptor This example shows how to use a request interceptor with OkHttp. This has numerous use cases such as:
See OkHttp topic (opens new window) for more details. |
| Header and Body: an Authentication Example The AuthenticaionRequest is our model, a POJO, containing the information the server requires. For this example, our server wants the client key and secret. Notice that in We create our Retrofit service somewhere. We make sure to use HTTPS. Then we can use our service. |
| Uploading a file via Multipart Declare your interface with Retrofit2 annotations: Where Building the implementation of your API and enqueue the call: |
| Retrofit 2 Custom Xml Converter Adding dependencies into the build.gradle file. Then create Converter Factory create a class to handle the body request. create a class to handle the body response. So, this point we can send and receive any XML , We just need create XStream Annotations for the entities. Then create a Retrofit instance: |
| Reading XML form URL with Retrofit 2 We will use retrofit 2 and SimpleXmlConverter to get xml data from url and parse to Java class. Add dependency to Gradle script: Create interface Also create xml class wrapper in our case Rss class Xml read function This is example of Java class with SimpleXML annotations More about annotations SimpleXmlDocumentation (opens new window) RemarksDependencies for the retrofit library: From the official documentation (opens new window): Gradle: Maven: |
Read Article
https://develop.horje.com/learn/1434/reference