大家好,我正在使用Volley MultipartRequest将多个图像上传到服务器,我想显示上传到服务器的文件数量以及剩余文件的进度。这是我上传多个图像的方法
public void apiCall2()
{
String url= BaseUrl.baseUrl+"addcart";
progressDialog.show();
final VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
String resultResponse = new String(response.data);
try {
progressDialog.cancel();
JSONObject result = new JSONObject(resultResponse);
Log.i("result",result.toString());
String status = result.getString("success");
String message = result.getString("msg");
if(status.equalsIgnoreCase("true"))
{
Toast.makeText(UploadImageActivity.this,"Images Uploaded Successfully !", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(UploadImageActivity.this,CartActivity.class);
startActivity(intent);
}
else
Toast.makeText(UploadImageActivity.this, message, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
String errorMessage = "Unknown error";
if (networkResponse == null) {
if (error.getClass().equals(TimeoutError.class)) {
errorMessage = "Request timeout";
} else if (error.getClass().equals(NoConnectionError.class)) {
errorMessage = "Failed to connect server";
}
} else {
String result = new String(networkResponse.data);
try {
JSONObject response = new JSONObject(result);
String status = response.getString("status");
String message = response.getString("message");
if (networkResponse.statusCode == 404) {
errorMessage = "Resource not found";
} else if (networkResponse.statusCode == 401) {
errorMessage = message+" Please login again";
} else if (networkResponse.statusCode == 400) {
errorMessage = message+ " Check your inputs";
} else if (networkResponse.statusCode == 500) {
errorMessage = message+" Something is getting wrong";
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.i("Error", errorMessage);
error.printStackTrace();
}
})
{
public Map<String,String> getHeaders() throws AuthFailureError
{
Map<String,String> headers=new HashMap<String,String>();
SharedPreferences prefs=getSharedPreferences("token", Context.MODE_PRIVATE);
String token=prefs.getString("value","wer");
headers.put("Content-Type", "multipart/form-data; boundary="+VolleyMultipartRequest.boundary);
headers.put("X-Requested-With","XMLHttpRequest");
headers.put("Authorization","Bearer "+token);
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("type",typeId);
params.put("size",sizeId);
params.put("manual_image","0");
if(coverId!=null)
{
params.put("cover",coverId);
}
return params;
}
@Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
for (int i = 0; i < path.size(); i++) {
Uri uri= path.get(i);
String filename=getFileName(uri);
Log.i("fff",filename);
try {
params.put("photos[" + i + "]", new DataPart(filename, data[i], "image/*"));
Log.i("filess",data[i].toString());
Log.i("cc",String.valueOf(count));
} catch (Exception e) {
e.printStackTrace();
}
}
return params;
}
};
multipartRequest.setRetryPolicy(new DefaultRetryPolicy(
3000,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance(this).addToRequestQueue(multipartRequest);
}
最后,我想显示进度条或进度对话框,例如 正在上传图片(1/20),并且在上传一张图片(2/20)等之后,请有人帮助。我搜索了很多但没有发现