Reputation: 115
I'm trying to send multiple image to server using retrofit what i'am doing is to send a map of RequestBody and this is my code
@Multipart
@POST("imageuload")
Call<ResponseBody> postImage(@PartMap Map<String, RequestBody> files );
and in my activity
Map<String, RequestBody> filestosend = new HashMap<>();
for (int pos = 0; pos < files.size(); pos++) {
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), files.get(pos));
filestosend.put("photo_" + String.valueOf(pos + 1), requestBody);
}
Call<ResponseBody> call = apiSerice.postImage(filestosend);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
Toast.makeText(getBaseContext(),response.body().string(),Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage(response.errorBody().string());
alert.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
and when i want to test for what i'am getting it return an empty respons from server and i get nothing in my response ..
echo file_get_contents('php://input');
i even tested with a single request body
RequestBody test = RequestBody.create(MediaType.parse("text/plain"), "test");
Call<ResponseBody> call = apiSerice.postImage(test);
but i am still getting an empty response in my on response i will appreciate any help or comment
Upvotes: 2
Views: 1167
Reputation: 3759
Multipart uploads can be accessed in PHP through $_FILES
. The PHP manual has the following to say regarding php://input
:
php://input is not available with enctype="multipart/form-data".
A full working example (minus correct endpoint URL, hard coded byte arrays):
public class Sample {
interface SampleService {
@Multipart
@POST("/test.php")
Call<ResponseBody> postImage(@Part List<MultipartBody.Part> files);
}
public static void main(String[] args) throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://...").build();
SampleService service = retrofit.create(SampleService.class);
RequestBody file1 = RequestBody.create(MediaType.parse("image/jpeg"), new byte[]{0x00});
MultipartBody.Part part1 = MultipartBody.Part.createFormData("A kitten", "Kitten.jpg", file1);
RequestBody file2 = RequestBody.create(MediaType.parse("image/jpeg"), new byte[]{0x00});
MultipartBody.Part part2 = MultipartBody.Part.createFormData("Another kitten", "Kitten2.jpg", file2);
System.out.println(service.postImage(Arrays.asList(part1, part2)).execute().body().string());
}
}
Server code:
<?php var_dump($_FILES); ?>
Output from client:
array(2) {
["A_kitten"]=>
array(5) {
["name"]=>
string(10) "Kitten.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/phpml5PIP"
["error"]=>
int(0)
["size"]=>
int(1)
}
["Another_kitten"]=>
array(5) {
["name"]=>
string(11) "Kitten2.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/phpzhgXm0"
["error"]=>
int(0)
["size"]=>
int(1)
}
}
Upvotes: 3