enter image description here I'm setting up the file uploading function using retrofit but I am getting internal server error. Sometime file is uploading on the server with details but response is coming in error body. I have no idea is there any problem in my code or in server side code.
I have tried different code for request body.
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(), requestFile);
File file = new File(PathHolder);
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("pic",
file.getName(), requestFile);
RequestBody userId =
RequestBody.create(MediaType.parse("text/plain"), uid);
RequestBody topic = RequestBody.create(MediaType.parse("text/plain"),
topicName);
RequestBody classID =
RequestBody.create(MediaType.parse("text/plain"), classId);
RequestBody shiftID =
RequestBody.create(MediaType.parse("text/plain"), shiftId);
Call<UploadDocumentResponse> resultCall = service.uploadImage(userId,
topic, classID, shiftID, body);
resultCall.enqueue(new Callback<UploadDocumentResponse>() {
@Override
public void onResponse(Call<UploadDocumentResponse> call,
Response<UploadDocumentResponse> response) {
progressDialog.dismiss();
onBackPressed();
UploadDocumentResponse iresponse = response.body();
// ViewDocModel Success or Fail
if (response.isSuccessful()) {
Toast.makeText(getApplicationContext(), response.body().
getMsg(), Toast.LENGTH_LONG).show();
imageView.setImageDrawable(null);
imagePath = null;
}
}
@Override
public void onFailure(Call<UploadDocumentResponse> call, Throwable t)
{
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "couldn't upload file,
please try
again", Toast.LENGTH_LONG).show();
}
});
// APIInterface
@Multipart
@POST("Camp/API_uploadfile.php")
Call<UploadDocumentResponse> uploadImage(@Part("uid") RequestBody uid,
@Part("topic") RequestBody
topic,
@Part("classid") RequestBody
classId,
@Part("shiftid") RequestBody
shiftId,
@Part MultipartBody.Part file);
Expected result is file should be upload on server and get response in success. Actual result is getting internal server error and sometime getting success response message in error body.
I have solved this issue. I was using wrong pattern of request body. I had to use multipart form data instead of text.
File file1 = new File(imageFilePathFront);
RequestBody requesFile = RequestBody.create(MediaType.parse("multipart.form-
data"), file1);
cnicFront = MultipartBody.Part.createFormData("CNIC_FRONT_IMG",
file1.getName(), requesFile);
This is the right code worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.