[JAVA] POST images from Android to PHP using Retrofit

Background

Research has made it necessary to POST images from Android to PHP on Azure It seems to be fun to use without thinking about asynchronous processing! !! I adopted Retrofit 2 because of the short-circuit thinking, but I was addicted to it unexpectedly, so a memo

Premise

--The following code is written on the assumption that it will be POSTed to http://example.com/upload/index.php --Permissions such as network access and access to internal storage are assumed to be given.

Library import

--Add the following sentence at the bottom of the familiar app / build.gradle

app/build.gradle


dependencies {
  :
  :
  compile 'com.squareup.retrofit2:retrofit:2.1.0+'
}

interface implementation

--Since the base URL is specified separately, Path is relative and OK

UploadService.class


public interface UploadService {
  @Multipart

  //Path can be the one without the base URL
  @POST("upload/index.php")
  Call<ResponseBody> upload(@Part MultipartBody.Part file);
}

ServiceGenerator -Quoted from Official -** Only the base URL part needs to be rewritten **

ServiceGenerator.class


public class ServiceGenerator {
  
  //Specify the base URL here
  public static final String API_BASE_URL = "http://example.com/";

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

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

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

Upload process

UploagService service = ServiceGenerator.createService(UploadService.class);  

//Files such as images, music, videos, etc. to be POSTed
String filePath = ”path/to/your/file”;
File file = new File(filePath);


RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("POST destination field name", file.getName(), requestFile);

Call<ResponseBody> call = service.upload(body);
call.enqueue(new Callback<ResponseBody>() {

  //Called when the status code is other than an error code such as 400
  @Override
  public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    //Processing on success
    // response.body();You can get inside the body tag of the HTML response with
  }
  
  //Called when the status code is an error code such as 400
  @Override
  public void onFailure(Call<ResponseBody> call, Throwable t) {
    //Processing at the time of failure
  }
});

Reference site

Recommended Posts

POST images from Android to PHP using Retrofit
[Android] Uploading images from your device to the server
[rails] How to post images
Android app to select and display images from the gallery
[Rails] How to upload images using Carrierwave
[Android] How to pass images and receive callbacks when sharing using ShareCompat
* Android * Saving / loading images to external storage
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Try using the Emotion API from Android
(Android) Try to display static text using DataBinding
[Rails] How to upload multiple images using Carrierwave
How to link images using FactoryBot Active Storage
Minecraft BE server development from PHP to Java
How to post images on Heroku + CarrierWave + S3
How to download images from AWS S3 (rails, carrierwave)
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
[Rails 6] Add images to seed files (using Active Storage)
Generate models from JSON to Swift, PHP, C #, JAVA
Push Docker images from GitHub Actions to GitHub Container Registry