Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 18.104.22.168 release ready!
If you are looking for one of the latest libraries for making HTTP connection in Android or Java, then you are at the right place. Retrofit is new library provide by Square Inc which is much better than its competitors in the market and also easy to use.
To use this library you need to add the dependency in your module level build Gradle
Here We have added four dependencies. The first one is to create HTTP connection using retrofit, the second one is to use Gson to convert response data directly into model which are used during data binding, the third one is to log the requests and the fourth one is actually used to create HTTP connection but we have used it to manage the cookies in our requests.
To make HTTP requests, it’s better to make a retrofit client which can be used again and again for all the requests instead of writing the same code again everywhere to make each HTTP request.
Here is an example of retrofit client which we have created
The above retrofit client can be used in the whole android project to make requests.
Next, you have to create the request
Your request method can be of any type like GET, POST, PUT, DELETE, and HEAD. Retrofit provides annotation for every request method.
If you notice in the above example, the request contains some header and fields ( i.e. Form Data ). The annotation at the top of the request ( @FormUrlEncode ) provides you the facility to encode form data in the request. The annotation @POST defines the method type followed by the request URL which does not contain base URL. The model your_reponse_data model which is mostly used when you have implemented data binding. To create a model you can take the help of jsonschema2pojo, all you need to do is insert you response data and it will automatically create a data model for you with setters and getters.
Now when you request is ready its time to make some requests. So in your Class, you need to take the instance of your retrofit client, call the request method using the retrofit client and pass the parameters in it. And also define a callback method which will handle the response of your request.
Look at the code segment below for better understanding :
And your callback method should look like this :
response.body() will provide you the object of your_response_data_model and using its getter and setter you can easily get and set the data from and into the model.
That’s all you need to create an HTTP request using Retrofit library.
Thank you very much. This is Vedesh Kumar signing off.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.