Как отправить пустой файл на сервер с помощью Android VolleyMultipart
У меня проблема при обновлении моего профиля с пустой фотографией. Я получаю 500 ошибок при отправке запроса. вот мои данные почтальона
ниже мой код
hashMap.put("categoryId", String.valueOf(myAdtactModelObject.getCategoryId())); hashMap.put("discountPercentage", скидка); hashMap.put ("описание", описание);
HashMap<String, List<File>> fileMap = new HashMap();
List<File> fileList = new ArrayList<>();
if (finalFile.exists()) {
fileList.add(finalFile);
} else {
fileList.add(null);
}
if (!finalFile.exists()) {
fileMap.put("file", fileList);
} else {
fileMap.put("file", fileList);
}
VolleyHelperMultiPart.postMultipartMultipleFiles(getActivity(), Constants.UPDATE_EXISTED_DISCOUNTED_OFFER,
hashMap, 0, null, fileMap,
Constants.REQUEST_OFFERING_FREE_OFFER, null);
ниже мой класс помощника залпа
public static void postMultipartMultipleFiles(final Context context, String serverUrl,
final Map<String, String> params, int fileType,
final Map<String, String> authenticationHeaders,
final Map<String, List<File>> fileMap,
final String reqType, final String localId) {
try {
if (NetWorkConnection.isConnected()) {
if (!reqType.equalsIgnoreCase(Constants.NOTIFICATION_UPLOAD_INTENT_SERVICE_FILES_REQUEST)) {
progressDialog = Utilities.showProgressDialog(context);
progressDialog.show();
}
RequestQueue queue = Volley.newRequestQueue(context.getApplicationContext());
final String url = Constants.SERVER_URL + serverUrl;
System.out.println("========server url was============" + url);
MultipartAPI multipartAPI = new MultipartAPI(url, params, authenticationHeaders, fileType, fileMap, reqType,
new Response.ErrorListener() {
ниже класс MultipartApi
public MultipartAPI(строковый URL, окончательные параметры карты, окончательная карта secureHeaders, int fileType, окончательная карта> fileParams, строковый reqType, Response.ErrorListener errorListener, Response.Listener listener) {
super(Method.POST, url, errorListener);
mListener = listener;
mParams = params;
msecureHeaders = secureHeaders;
this.fileType = fileType;
mFileParams = fileParams;
this.reqType = reqType;
mBuilder = MultipartEntityBuilder.create();
buildMultipartEntity();
}
/**
* AuthFailureError
*
* @return
* @throws AuthFailureError
*/
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
if (msecureHeaders != null) {
return msecureHeaders;
} else {
return super.getHeaders();
}
}
/**
* @return
*/
@Override
public String getBodyContentType() {
return mBodyContentType;
}
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
System.out.println("getting Body MultiPart----->");
HttpEntity entity = mBuilder.build();
mBodyContentType = entity.getContentType().getValue();
entity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e(e.getMessage());
}
return bos.toByteArray();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
/**
*
*/
private void buildMultipartEntity() {
if (mParams != null) {
for (Map.Entry<String, String> entry : mParams.entrySet()) {
mBuilder.addTextBody(entry.getKey(), entry.getValue());
}
}
//System.out.println("Building MultiPart----->" + mFileParams.size());
if (mFileParams != null) {
for (Map.Entry<String, List<File>> entry : mFileParams.entrySet()) {
List<File> listFiles = entry.getValue();
for (File file : listFiles) {
if (file == null) {
Log.d("tag", "my condition");
mBuilder.addPart("file",new FileBody(new File(""),ContentType.create("image/jpg"),"") );
// mBuilder.addBinaryBody("file", new File(""), ContentType.create("image/jpg"), "");
} else {
if (reqType.equalsIgnoreCase(Constants.PROFILE_IMAGE_UPLOAD_RESPONSE)) {
if (fileType == 1) {
System.out.println("========================Image uploading");
mBuilder.addBinaryBody(entry.getKey(), file, ContentType.create("image/jpg"),
file.getName());
System.out.println("=========get key======" + entry.getKey());
}
} else {
if (entry.getKey().equalsIgnoreCase("file")) {
mBuilder.addBinaryBody(entry.getKey(), file, ContentType.create("image/jpg"),
file.getName());
}
}
}
}
}
}
mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
}
Ошибка 500
E/Volley: [803] MultipartAPI.getBody: : open failed: ENOENT (No such file or directory)
E/Volley: [803] BasicNetwork.performRequest: Unexpected response code 500
=======error response========dfbrgbvra======<!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad Request</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 400 – Bad Request</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Required request part 'file' is not present</p><p><b>Description</b> The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).</p><hr class="line" /><h3>Apache Tomcat/8.5.23</h3></body></html>
Пожалуйста, кто-нибудь, помогите мне решить мою проблему.