0
Android 용 Retrofit을 사용하여 업로드 이미지를 만들려고합니다. 하지만 여전히 내 앱 오류가 발생합니다.Android - Multipart Retrofit 실패 또는 충돌로 이미지 업로드
이 이미지
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에서 응답이 없기 때문에 오류가 발생합니다. d 오류. 제발 도와주세요. 감사.
어떤 오류가 발생 했습니까? – yosriz
api의 응답이 "파일을 찾을 수 없습니다"이며 디버거에서 볼 수 있습니다. contentType은 null입니다. 그것은 "DocTyp01"을 작성해야합니다. Rest Client @yosriz –
오류 로그를 첨부하십시오. – yosriz