how to config retrofit work like image below
this is my code:
// interface ImageUploadService
#Multipart
#POST("/api=upl_img_version_2&token={token}&img_cat=3&sum={sum}")
Observable<ServerResponse> uploadAvatar(#Path("token") String token, #Path("sum") String sum, #Part MultipartBody.Part file);
===========================================
//File creating from selected URL
File file = new File(path);
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// body part send to server
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
view.showLoadingDialog();
imageUploadService.uploadAvatar(token, sum, body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ServerResponse>() {
#Override
public void onCompleted() {
view.hideLoadingDialog();
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
view.hideLoadingDialog();
}
#Override
public void onNext(ServerResponse serverResponse) {
Log.d("aaaaa", serverResponse.toString());
}
})
then use burp suite to capture request and it's different with ios (working), i don't have any idea or keyword about that. Thanks
i've just found a solution and want to help someone face same issue instead of delete this question.
here is working code:
interface ImageUploadService {
//#Multipart
#POST("/api=upl_img_version_2&token={token}&img_cat=3&sum={sum}")
Observable<ServerResponse> uploadAvatar(#Path("token") String token, #Path("sum") String sum, #Body RequestBody file); // #Part => #Body
}
///////////////////////////////
File file = new File(path);
// MediaType.parse("multipart/form-data")
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
imageUploadService.uploadAvatar(token, sum, requestBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ServerResponse>() {
#Override
public void onCompleted() {
view.hideLoadingDialog();
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
view.hideLoadingDialog();
}
#Override
public void onNext(ServerResponse serverResponse) {
Log.d("aaaaa", serverResponse.toString());
}
})
Related
I am trying to upload a video file using multipart by retrofit and I get the following error in response
Response{protocol=http/1.1, code=405, message=Method Not Allowed, url=serverurl/Upload/Videos/}
This is my code for file upload
void uploadVideo(String videoPath) {
dialog.show();
File videoFile = new File(videoPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), videoPath);
MultipartBody.Part vFile = MultipartBody.Part.createFormData("file", videoFile.getName(), requestFile);
apiCall.uploadVideoToServer(presenter.getUserInfo().getUploadVideoPath(), vFile).enqueue(new Callback<String>() {
#Override
public void onResponse(Call<String> call, Response<String> response) {
dialog.dismiss();
}
#Override
public void onFailure(Call<String> call, Throwable t) {
dialog.dismiss();
}
});
}
This is the path I am uploading the file to
http://serverurl.com/Upload/Videos/
Can someone tell me whats wrong with my code?
Retrofir API interface
#Multipart
#POST
Call<String> uploadVideoToServer(#Url String url, #Part MultipartBody.Part video);
in your api service interface :
#Multipart
#POST("Upload/Videos") // *** DONT FORGET UR END POINT HERE ***
Call<urmodel> uploadVideoToServer(#PartMap Map<String, RequestBody> map);
and write a method to upload ur video file like this :
private void uploadVideoToServer(String path) {
File videoFile = new File(path);
if (videoFile.exists()) {
//create request body
Map<String, RequestBody> map = new HashMap<>();
RequestBody requestBody = RequestBody.create(MediaType.parse("video/*"), videoFile);
map.put("video\"; filename=\"" + videoFile.getName() + "\"", requestBody);
//make call
Call<urmodel> call = mTService.uploadVideoToServer(map);
call.enqueue(new Callback<urmodel>() {
#Override
public void onResponse(Call<urmodel> call, Response<urmodel> response) {
if (response.isSuccess()) {
// DO SOMETHING
} else {
// DO SOMETHING
}
}
#Override
public void onFailure(Call<urmodel> call, Throwable t) {
//occur when fail to deserialize || no network connection || server unavailable
Toast.makeText(getBaseContext(), "Fail it >> " + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(getBaseContext(), "can not upload file since the file is not exist :|", Toast.LENGTH_SHORT).show();
}
}
I'm trying to make a file upload with a picture and an string parameter, the file gets upload successfuly, but the string paremeter is always null. Could someone else help me please? Thanks in advance!
//Retrofit Interface
#Multipart
#POST("account/imageupload")
Call<ResponseBody> uploadProfilePicture(#Part MultipartBody.Part file,
#Part("userid") RequestBody userid);
//Activity
ApiClient apiClient = new ApiClient(token.getAccessToken(), "multipart/form-data");
ApiInterface apiService = apiClient.getClient().create(ApiInterface.class);
File file = new File(selectedImagePath);
RequestBody reqFile = RequestBody.create(MediaType.parse(getContentResolver().getType(selectedImageUri)), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
RequestBody user = RequestBody.create(MultipartBody.FORM, token.getUserId());
Call<ResponseBody> call = apiService.uploadProfilePicture(body, user);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
int statusCode = response.code();
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
String message = t.getMessage();
}
});
//Web API (C# web api)
[Route("imageupload")]
[HttpPost]
public async Task<IHttpActionResult> UploadProfilePicture([FromBody]
Models.ProfilePhotoUpload photo)
{
WriteImage(photo.file);
return Ok();
}
//Api Controller Model
public class ProfilePhotoUpload
{
string userid { get; set; }
public HttpFile file { get; set; }
}
The problem is that the file gets uploaded correctly, but the userid parameter is always null.
You are creating incorrect RequestBody for text
Try this:
RequestBody user = RequestBody.create(MediaType.parse("text/plain"), token.getUserId());
I am trying to get my Retrofit2 to upload an image to my Flask server, I am not sure why it is not sending the parameter "file" when sending the request.
APIInteface.java
#Multipart
#PUT("api/user/update/")
Call<ResponseBody> UpdateUser(#QueryMap Map<String,String> options,
#Part MultipartBody.Part image, #Part("file") RequestBody file);
MainActivity:
File file = new File(filePath);
Log.d(TAG,"Calling update function");
update(file);
}
}
public void update(File file) {
Log.d(TAG,"I got called, yey!");
ApiInterface apiService = ApiClient.GetAuthClient(token).create(ApiInterface.class);
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), "upload_test");
data.put("uname",username.getText().toString());
data.put("fname",firstname.getText().toString());
data.put("lname",lastname.getText().toString());
data.put("address",address.getText().toString());
data.put("password",password.getText().toString());
data.put("preferance1",String.valueOf(spinner1.getSelectedItem()));
data.put("preferance2",String.valueOf(spinner2.getSelectedItem()));
data.put("preferance3",String.valueOf(spinner3.getSelectedItem()));
Call<ResponseBody> call = apiService.UpdateUser(data,body,name);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
Rxphoto: How can i use .requestUri(context, TypeRequest.GALLERY) whit RxJava?
Link ti library: https://android-arsenal.com/details/1/3870#!description
And how then upload this file using Retrofit2? Some examples?
Thanks!
Use this this
RxPhoto.requestUri(context, TypeRequest.GALLERY)
flatMap(new Func1<Uri, Observable<Response>>() {
#Override
public Observable<Response> call(Uri uri) {
File file = new File(uriString);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload_test");
return service.postImage(body, name);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Response>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(Response response) {
}
});
The Retrofit call service
interface Service {
#Multipart
#POST("/")
Observable<Response> postImage(#Part MultipartBody.Part image,#Part("name") RequestBody name);
}
Note: Service should be multipart request compulsory in Server Side
I'm sending object in request body, something like that :
{
"title":"test",
"description":"test",
"images":[]
}
#POST("create-data")
Call<JsonObject> publishData(#Body MyObject object);
and it's work fine without the images. From the docs I can find how to upload file to server using MultipartBody.Part, my questions is :
How can I upload multiple images at the same time?
Is it possible to send the images inside the object, or I need to send it separately and how ?
thank you very much.
request success just now with server
I reference the article:
https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server
#Multipart
#POST("uploadHeadPic")
Call<UploadHeadPicResponseModel> uploadHeadPic(#Part MultipartBody.Part file, #Part("json") RequestBody json);
public void doUploadHeadPic(#NonNull String filePath) {
if (!MNetworkUtil.isNetworkAvailable()) {
MToastUtil.show("网络不能连接");
return;
}
File file = new File(filePath);
String json = new Gson().toJson(new UploadHeadPicRequestModel());
if (!file.exists()) {
MToastUtil.show("文件不存在");
return;
}
progressDialog.show();
avatarSimpleDraweeView.setEnabled(false);
MApiManager.getService().uploadHeadPic(
MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)),
RequestBody.create(MediaType.parse("multipart/form-data"), json))
.enqueue(new OnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) {
#Override
public void onSuccess(UploadHeadPicResponseModel responseModel) {
progressDialog.dismiss();
avatarSimpleDraweeView.setEnabled(true);
if (responseModel != null) {
String serverAvatarUrl = responseModel.data.headPicPath;
if (!TextUtils.isEmpty(serverAvatarUrl)) {
UserModel userModel = MUserManager.getInstance().getUser();
if (userModel != null) {
userModel.setAvatarUrl(serverAvatarUrl);
MUserManager.getInstance().updateOrInsertUserInfo(userModel);
MToastUtil.show("上传头像成功");
}
}
}
}
#Override
public void onFailure(int status, String failureMsg) {
progressDialog.dismiss();
avatarSimpleDraweeView.setEnabled(true);
MToastUtil.show((TextUtils.isEmpty(failureMsg) ? "上传失败" : failureMsg) + " : " + status);
}
});
}
update for multi files
may be this could help , I did not try
#Multipart
#POST("uploadHeadPic")
Call<UploadHeadPicResponseModel> uploadHeadPic(#Part MultipartBody.Part file0, #Part MultipartBody.Part file1, #Part("json") RequestBody json);
public void doUploadHeadPic(#NonNull String filePath) {
MApiManager.getService().uploadHeadPic(
MultipartBody.Part.createFormData("file0", file0.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file0)),
MultipartBody.Part.createFormData("file1", file1.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file1)),
RequestBody.create(MediaType.parse("multipart/form-data"), json))
.enqueue(new OnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) {
#Override
public void onSuccess(UploadHeadPicResponseModel responseModel) {
}
#Override
public void onFailure(int status, String failureMsg) {
}
});
}
In my case (uploading to server built with Spring) I needed to change MediaType for RequestBody:
RequestBody.create(MediaType.parse("application/json"), json)
RequestBody.create(MediaType.parse("image/jpg"), file)