Reputation: 363
I am currently working on a part of the application where user need to choose some images from gallery and then upload those images to a server. for the upload i am using retrofit 2.0 using multipart.
Following the resource https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server i managed to implement it successfully when using only one file, however i would like that on one session all images user chose to be uploaded to the server.
Looking around I found "Retrofit" multiple images attached in one multipart request where it discuss on @partmap annotation and it seems like the proper match however i am failing to understand how i will iterate over the images I need to upload?
can someone point me in to the right implementation?
Also checked: 1.Retrofit(2.0 beta2) Multipart file upload doesn't work 2.Upload multiple image with same name as array Retrofit 2.0.0-beta2
Upvotes: 1
Views: 2778
Reputation: 2789
Wooh! I Did, You May Try this as i did ( Retrofit 2 )
//1. What We Need From Server ( upload.php Script )
public class FromServer {
String result;
}
//2. Which Interface To Communicate Our upload.php Script?
public interface ServerAPI {
@Multipart
@POST("upload.php")//Our Destination PHP Script
Call<List<FromServer>> upload(
@Part("file_name") String file_name,
@Part("file") RequestBody file);
Retrofit retrofit =
new Retrofit.Builder()
.baseUrl("http://192.168.43.135/retro/") // REMEMBER TO END with /
.addConverterFactory(GsonConverterFactory.create())
.build();
}
//3. How To Upload
private void upload(){
ServerAPI api = ServerAPI.retrofit.create(ServerAPI.class);
File from_phone = FileUtils.getFile(Environment.getExternalStorageDirectory()+"/aa.jpg"); //org.apache.commons.io.FileUtils
RequestBody to_server = RequestBody.create(MediaType.parse("multipart/form-data"), from_phone);
api.upload(from_phone.getName(),to_server).enqueue(new Callback<List<FromServer>>() {
@Override
public void onResponse(Call<List<FromServer>> call, Response<List<FromServer>> response) {
Toast.makeText(MainActivity.this, response.body().get(0).result, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<FromServer>> call, Throwable t) { }
});
}
//4. upload.php
<?php
$pic = $_POST['file_name'];
$pic = str_replace("\"", "", $pic); //REMOVE " from file name
$f = fopen($pic, "w");
fwrite($f,$_POST['file']);
fclose($f);
$arr[] = array("result"=>"Done");
print(json_encode($arr));
?>
Upvotes: 1
Reputation: 363
Since I didnt manage to resolve the issue using retrofit I ended up using okhttp library that gave me what i needed, here is an example of the code I use which I hope can help someone
build the request body:
okhttp3.RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(okhttp3.Headers.of("Content-Disposition:", "form-data; name=\"myfile\"; filename=\"image" + ImageNumber + ".jpg\""),
okhttp3.RequestBody.create(MEDIA_TYPE_JPG, new File(ImagePath)))
.build();
prepare the request:
request = new okhttp3.Request.Builder()
.header("Authorization", "Client-ID " )
.url(YOUR URL)
.post(requestBody)
.build();
initiate connection to server:
try {
okhttp3.Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 437
I'm just trying to solve a similar case, but what I have is a great amount of files.
I've tried to solve it using a loop:
recordsDirectory.mkdirs();
final File[] list_files = recordsDirectory.listFiles();
int cont = list_files.length;
for (int i = 0; i < cont; i++) {
final File ref_file = list_files[i];
String source_filepath = ref_file.getPath();
RequestBody requestBody =
RequestBody.create(MediaType.parse("multipart/form-data"), ref_file);
Call<DeliveryResponse> call = apiService.upload(requestBody, description);
call.enqueue(new Callback<DeliveryResponse>() {
@Override
public void onResponse(Response<DeliveryResponse> response, Retrofit retrofit) {
Log.v(TAG, "[" + TAG + "] Upload success. status: " + response.toString());
DeliveryResponse deliveryResponse = response.body();
Log.v(TAG, "response code: " + deliveryResponse.mStatus);
Log.v(TAG, "response code: " + response.code());
Log.v(TAG, "ref_file: " + ref_file.getPath());
if (response.isSuccess()) {
Log.i(TAG, "[" + TAG + "] The server has response with a status distinct to 200.");
}
else {
if (deliveryResponse.mStatus == 1) {
Log.i(TAG, "[" + TAG + "] The server has response with 1 as status.");
boolean deleted = ref_file.delete();
} else {
Log.i(TAG, "[" + TAG + "] The server has response with 0 as status.");
}
}
}
@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.getMessage());
}
});
}
/***********************************************
RETROFIT API
************************************************/
public interface MyApiEndpointInterface {
@Multipart
@POST("/elderbask/api/sensors/accelerometer")
Call<DeliveryResponse> upload(
@Part("myfile\"; filename=\"image.png\" ") RequestBody file,
@Part("description") String description
);
}
public static class DeliveryResponse {
@SerializedName("status")
int mStatus;
public DeliveryResponse(int status) {
this.mStatus = status;
}
public String toString(){
return "mStatus: " + this.mStatus;
}
}
But this solution is not working as good as I expect. I'm having many timeout exceptions, and the false hardcoded filename is quite awful.
Upvotes: 0