Now you need to create an input model class for image upload and for the retrofit response we need to create image output model class. Retrofit + GSON + Interceptor retrofit_version = '2.2.0'Ĭompile "2:retrofit:$retrofit_version"Ĭompile "2:adapter-rxjava2:$retrofit_version"Ĭompile "2:converter-gson:$retrofit_version"Ĭompile '3:logging-interceptor:3.6.0' Ok, Let’s see how to upload an image file to the server by using retrofit? First of all, we need to add the retrofit dependency file in your app grade file. RxJava2 in Android Part-1, RxJava in Android part-2 and Understanding Java 8 Stream and RxJava2 Observable in Android. Please check this post to understand that how to use API call by using retrofit in Kotlin? Please check these post of reactive nature to better understand. Android developers love this because it gives a really good response to become your app reactive. Now a day Reactive is getting popular in android application development. RxAndroid provides the Android Scheduler for scheduling the main thread switch between the threads easily. Retrofit works very seamlessly with RxJava. Retrofit is not only simple to integrate but also provide many features for handling multiple requests like Dispatcher, caching, and easy to handle the error. Retrofit used OkHttp request call which is extended and customized of Http request.Īndroid developers love this because it is pretty simple and easy to integrate into an android application. It is very simple and cleaner way API call in the Android application. Retrofit is the most popular for networking API call. You can add GSON converters, XML converters or even custom converters.Ĭheck out the REST API course using Retrofit2 and MVVM architecture: here.In this tutorial, we will learn how to upload a file on the server by using Retrofit. One of the most important aspects of retrofit is the hassle-free setup provided by retrofit to integrate different converters used for data serialization. Create a class RecyclerviewAdapter.java and paste the following code in it.įinally, we will initialize the Endpoints interface and the Recyclerview in the MainActivity. Now we will create the Adapter class for our recyclerview. Here we will provide the details about the parameters and request method. To issue network requests to a REST API with Retrofit, we need to create an instance using the RetrofitClientInstance class and configure it with a base URL.Ĭreate a class RetrofitClientInstance.java, here with the URL of our API we will make a call. Later we've set getter and setter for all the variables. For example, the Java field title is represented as a name in JSON. The parameter (value) of this annotation is the name to be used when serializing and deserializing objects. These fields are annotated with the annotation. Posts class has fields that represent the post title and body of a post. Make a model class Posts.java and put the following code inside it. You can check them hereĪdd the internet permission in your AndroidManifest.xml Make sure you have the latest dependencies. Open adle (Module:app) and add Retrofit, GSON, RecyclerView and CardView dependencies. Retrofit easily translates JSON or XML response to POJO’s (Plain Old Java Objects). Retrofit Uses OkHttp for making HTTP requests. Retrofit can be used as the best alternative to Volley. Using Retrofit we can easily translate the REST APIs to Java Interfaces. Retrofit is a type-safe HTTP client for Android and Java developed by Square. In this blog I'm gonna show you how to work with the retrofit in android.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |