我将多个数据发送到服务器,如视频,图像和文本在我的情况下,当我上传所有数据时,它将被上传但是,在我的应用程序中,某些数据不是必需的所以它将是空白或NUll。 当我向Volly发送此类请求(空值或空白)时,它会从错误列表器中向我提供Toast。 在下面的代码(AdvertiseImage2ImageBitmap)将为null,我从错误列表器获取Toast 如何解决这个问题。我正在寻找2天。
private void uploadAdNewUser() {
String[] parts = Duration.split("\\:"); // escape .
Video_Min = parts[0];
Video_Sec = parts[1];
Utils.pdialogMk(AddAdvertisePublicity.this);
VolleyMultipartRequest volleyMultipartRequest = new VolleyMultipartRequest(Request.Method.POST, AppConstants.MainUrl + AppConstants.AddAdvertise,
new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
try {
JSONObject obj = new JSONObject(new String(response.data));
Utils.pdialog_dismissMk();
if (obj.getString(AppConstants.flag).equals("true")) {
Utils.ShowToast(obj.getString(AppConstants.message));
startActivity(new Intent(AddAdvertisePublicity.this, MainActivity.class));
//finish();
} else {
Utils.ShowToast(obj.getString(AppConstants.message));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Utils.pdialog_dismissMk();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
/*
* If you want to add more parameters with the image
* you can do it here
* here we have only one parameter with the image
* which is tags
* */
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> KeyParams = new HashMap<>();
KeyParams.put(AppConstants.name, Name);
KeyParams.put(AppConstants.address, Address);
KeyParams.put(AppConstants.email_id, Email);
KeyParams.put(AppConstants.birth_date, Dob);
KeyParams.put(AppConstants.mobile_no, MobileNo);
KeyParams.put(AppConstants.mob_imei, Imei1);
KeyParams.put(AppConstants.mob_imei2, Imei2);
KeyParams.put(AppConstants.age, Age);
KeyParams.put(AppConstants.height, Height);
KeyParams.put(AppConstants.chest, Chest);
KeyParams.put(AppConstants.waist, Waist);
KeyParams.put(AppConstants.weight, Weight);
KeyParams.put(AppConstants.price, Ad_Price);
KeyParams.put(AppConstants.sex, Gender);
KeyParams.put(AppConstants.video_min, "00:" + Video_Min + ":00");
KeyParams.put(AppConstants.video_second, "00:00:" + Video_Sec);
KeyParams.put(AppConstants.character, Character);
KeyParams.put(AppConstants.interest, Interest);
KeyParams.put(AppConstants.interest_notes, Interested_Notes);
KeyParams.put(AppConstants.state, SelectedState);
KeyParams.put(AppConstants.city, SelectedState);
return KeyParams;
}
/*
* Here we are passing image by renaming it with a unique name
* */
@Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
long AdImage1 = System.currentTimeMillis();
long AdImage2 = System.currentTimeMillis() + 1;
long VideoName = System.currentTimeMillis() + 3;
params.put(AppConstants.advertise_image1, new DataPart(AdImage1 + ".png", getFileDataFromDrawable(AdvertiseImage1ImageBitmap)));
params.put(AppConstants.advertise_image2, new DataPart(AdImage2 + ".jpg", getFileDataFromDrawable(AdvertiseImage2ImageBitmap)));
params.put(AppConstants.video, new DataPart(VideoName + ".mp4", Vidbuffer));
return params;
}
};
//adding the request to volley
Volley.newRequestQueue(this).add(volleyMultipartRequest);
volleyMultipartRequest.setRetryPolicy(mRetryPolicy);
}
答案 0 :(得分:0)
以下是对getByteData()方法的一些更改。 如下所列。
@Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
long AdImage1 = System.currentTimeMillis();
long AdImage2 = System.currentTimeMillis() + 1;
long VideoName = System.currentTimeMillis() + 3;
if (AdvertiseImage1ImageBitmap != null) {
params.put(AppConstants.advertise_image1, new DataPart(AdImage1 + ".png", getFileDataFromDrawable(AdvertiseImage1ImageBitmap)));
}
if (AdvertiseImage2ImageBitmap != null) {
params.put(AppConstants.advertise_image2, new DataPart(AdImage2 + ".jpg", getFileDataFromDrawable(AdvertiseImage2ImageBitmap)));
}
if (Vidbuffer != null) {
params.put(AppConstants.video, new DataPart(VideoName + ".mp4", Vidbuffer));
}
return params;
}