Android Retrofit 2 file upload
<p>I'm using Retrofit 2 and I want to call a web service to upload my images to my server. Server side i'm using <a href="https://phalconphp.com/en/" rel="nofollow">Phalcon PHP</a> and I check if I have a file to upload. But I have never a file to upload. I can get the description parameter but not the file parameter.</p>

<p>Could you tell me if my android code is ok ? I don't know what is wrong.</p>

<p><strong>ApplicationService</strong></p>

<pre><code>public interface ApplicationService {

...
// My other web services works well

@Multipart
@POST("/path/to/uploadPictures")
Call&lt;ResponseBody&gt; upload(@Part("description") RequestBody description,
                          @Part("file") RequestBody file);

}
</code></pre>

<p><strong>ServiceGenerator Class</strong></p>

<pre><code>public class ServiceGenerator {

public static final String API_BASE_URL = Globals.SERVER_NAME;

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder = new Retrofit.Builder()
                                                        .baseUrl(API_BASE_URL)
                                                        .addConverterFactory(GsonConverterFactory.create());

public static &lt;S&gt; S createService(Class&lt;S&gt; serviceClass) {
    Retrofit retrofit = builder.client(httpClient.build()).build();
    return retrofit.create(serviceClass);
}

}
</code></pre>

<p><strong>Upload file calling</strong></p>

<pre><code>private void uploadFile(String filename) {
    // create upload service client
    ApplicationService service = ServiceGenerator.createService(ApplicationService.class);

    File file = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);

    // create RequestBody instance from file
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // add another part within the multipart request
    String descriptionString = "hello, this is description speaking";
    RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

    // finally, execute the request
    Call&lt;ResponseBody&gt; call = service.upload(description, requestFile);
    call.enqueue(new Callback&lt;ResponseBody&gt;() {
        @Override
        public void onResponse(Call&lt;ResponseBody&gt; call,
                               Response&lt;ResponseBody&gt; response) {
            Log.v("Upload", "success");
        }

        @Override
        public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });
}
</code></pre>