haains.blogg.se

Retrofit file upload example
Retrofit file upload example









retrofit file upload example

If you wish to use a separate OkHttp dependency, you should exclude the OkHttp dependency from Retrofit 2 as:Ĭompile ('2:retrofit:2.1. In order to know the upload progress, we’ll use OkHttp. We hope that you’ve successfully set up the Node JS server in the previous tutorial. Retrofit MultiPart Image Upload Progress. Construct Retrofit call, postImage parameter & execute postImage. Non-file Part should use RequestBody type with expected fieldname. MultipartBody.Part is the type that we want to use for a file / image.

retrofit file upload example

OkHttp dependency is already shipped with Retrofit 2 dependency. In this tutorial, we’ll be implementing Image Uploading while showing the upload progress in our android application. In order to POST a file, we need to enable Multipart. We need to add the following dependencies in our ade file.Ĭompile '2:retrofit:2.1.0'Ĭompile ':gson:2.6.2'Ĭompile '2:converter-gson:2.1.0'

retrofit file upload example

To serialise JSON we need a converter to convert it into Gson first. Retrofit automatically serialises the JSON response using a POJO(Plain Old Java Object) which must be defined in advanced for the JSON Structure. This tutorial is intentionally separated from the already published tutorial on how to upload files with Retrofit v1, because the internal changes from Retrofit 1 to Retrofit 2 are profound and you need to understand the way Retrofit 2 handles file uploads. By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for Body. By default, Retrofit will give you sane defaults for your platform but it allows for customization. Retrofit 2 by default leverages OkHttp as the networking layer and is built on top of it. Retrofit is the class through which your API interfaces are turned into callable objects.











Retrofit file upload example