I have like 4 days, trying to make a Multipart Request using Retrofit 1.8.0 in android with any success. My interface looks something like this
@Multipart
@
I had similar problem today to send file and some fields, here is my solution
My interface, TypedFile is a Retrofit class
@Multipart
@POST("/api/Media/add")
void addMedia(@Part("file") TypedFile photo,
@Part("type") String type,
@Part("name") String name,
@Part("description") String description,
Callback callback);
in Activity
profileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
}
});
and then
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_GALLERY :
if (null != data) {
imageUri = data.getData();
String selectedImagePath = null;
Uri selectedImageUri = data.getData();
Cursor cursor = activity.getContentResolver().query(selectedImageUri, null, null,
null, null);
if (cursor == null) {
selectedImagePath = imageUri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
selectedImagePath = cursor.getString(idx);
}
File file = new File(selectedImagePath);
waiter.setVisibility(View.VISIBLE);
_YOUR_APP_._YOUR_INTERFACE_.addMedia(new TypedFile("image/*", file), "avatar", "avatar", "avatar", new Callback() {
@Override
public void success(Media media, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
});
}
break;
default:
break;
}
}
Media class is simple pojo to keep answer
public class Media {
@Expose
private int[] data;
/**
*
* @return
* The data
*/
public int getData() {
return data[0];
}
/**
*
* @param data
* The data
*/
public void setData(int[] data) {
this.data = data;
}
}