我和我的团队正在尝试使用改造。我想使用POST请求上传带有一些字符串值的图像。我不断得到错误的“文件名”是未定义的。这是对的吗?
private void uploadMultipart(File file) {
final String kodedokter = "D021422";
final String bulan = "12";
final String tahun = "2016";
RequestBody photoBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part photoPart = MultipartBody.Part.createFormData("sign",
file.getName(), photoBody );
RequestBody kodedokter1 = RequestBody.create(MediaType.parse("kode_dokter"), kodedokter);
RequestBody bulan1 = RequestBody.create(MediaType.parse("bulan"), bulan);
RequestBody tahun1 = RequestBody.create(MediaType.parse("tahun"), tahun);
uploadService = new UploadService();
uploadService.uploadPhotoMultipart(kodedokter1,bulan1,tahun1, photoPart, new Callback() {
@Override
public void onResponse(Call call, Response response) {
BaseResponse baseResponse = (BaseResponse) response.body();
if(baseResponse != null) {
Toast.makeText(MainActivity.this, baseResponse.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call call, Throwable t) {
t.printStackTrace();
}
});
}
答案 0 :(得分:0)
检查以下代码
private void sendSinglePhoto(String location, String longitude, String latitude, File singleImage) {
Constant.showProgressDialog(SinglePhotoActivity.this);
RequestBody Location = RequestBody.create(MediaType.parse("multipart/form-data"), location);
RequestBody Longitude = RequestBody.create(MediaType.parse("multipart/form-data"), longitude);
RequestBody Latitude = RequestBody.create(MediaType.parse("multipart/form-data"), latitude);
RequestBody ProfileImage;
MultipartBody.Part body;
if (singleImage != null) {
ProfileImage = RequestBody.create(MediaType.parse("image/*"), singleImage);
body = MultipartBody.Part.createFormData("single_image", singleImageFile.getName(), ProfileImage);
} else {
ProfileImage = RequestBody.create(MediaType.parse("multipart/form-data"), "");
body = MultipartBody.Part.createFormData("single_image", "", ProfileImage);
}
uploadSinglePhoto(this, Location, Longitude, Latitude, body);
}
public void uploadSinglePhoto(final NetworkResponseListener listener, RequestBody location, RequestBody longitude, RequestBody latitude, MultipartBody.Part singleImage) {
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RestClient.ROOT)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestClient.NetworkCall networkCall = retrofit.create(RestClient.NetworkCall.class);
Call<UploadPhotoResponse> response = networkCall.uploadSinglePhoto(Prefs.getAuth(App.getContext()), location, longitude, latitude, singleImage);
response.enqueue(new Callback<UploadPhotoResponse>() {
@Override
public void onResponse(Call<UploadPhotoResponse> call, Response<UploadPhotoResponse> response) {
if (response.code() == Constant.SUCCESS_STATUS) {
"Your Success Code should be here"
} else {
"Your Success Code should be here"
}
}
@Override
public void onFailure(Call<UploadPhotoResponse> call, Throwable t) {
}
});
}
@Multipart
@POST("your URL")
Call<UploadPhotoResponse> uploadSinglePhoto(@Header("Authorization") String auth, @Part("location") RequestBody location, @Part("longitude") RequestBody longitude, @Part("latitude") RequestBody latitude, @Part MultipartBody.Part singleImage);