【发布时间】:2017-07-14 19:06:43
【问题描述】:
我尝试使用 Retrofit Mulitpart Android 制作上传图片。但我仍然收到我的应用程序错误。
这是获取图片的方法
private final int SELECT_PHOTO = 1;
private void startGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/png");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
这是 onActivityResult 和我请求上传图片的方式
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
selectedImage = imageReturnedIntent.getData();
File file = new File(selectedImage.getPath());
RequestBody reqFile = RequestBody.create(okhttp3.MediaType.parse("image/png"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body = MultipartBody.Part.createFormData("DocTyp01", file.getName(), reqFile);
getApi().uploadByCustomer(PrefHelper.getString(PrefKey.TOKEN), "DocTyp01", body)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<GenericResponse>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
dismissProgressDialog();
Timber.e(e.getMessage());
}
@Override
public void onNext(GenericResponse response) {
dismissProgressDialog();
if (response.getCode() == 0) {
showSuccessDialog("Saving success", false);
}
}
});
}
}
}
这是 API 服务
@Multipart
@POST(PORTAL_URL + "customerDocument/uploadByCustomer")
Observable<GenericResponse> uploadByCustomer(@Part("token") String token,
@Part("type") String type,
@Part MultipartBody.Part requestBodyFile);
而且有时日志没有显示错误,而且我没有从 API 得到响应,这让我很难找到错误。请帮我解决这个问题。谢谢。
【问题讨论】:
-
你遇到了什么错误?
-
来自 api 的响应是“找不到文件”,我在调试器上看到 contentType 为空。如果我在 Rest Client @yosriz 上测试它应该填写“DocTyp01”
-
请附上错误日志
标签: android upload rx-java multipartform-data retrofit2