您好我正在尝试使用Volley库将大型视频文件上传到服务器但是当我向服务器请求大视频文件时,它需要花费太多时间并且视频无法上传存在服务器端限制 4MB
我如何解决这个问题,请帮我一些
RequestQueue queue = Volley.newRequestQueue(context.getApplicationContext());
final String url = Constants.SERVER_URL + serverUrl;
MultipartAPI multipartAPI = new MultipartAPI(url, params, authenticationHeaders,fileType,fileMap, reqType,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyErrorvolleyError) {
// progressDialog.dismiss();
if (volleyError != null) {
}
}
}, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("------->" + response);
}
});
int socketTimeout = 450000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
multipartAPI.setRetryPolicy(policy);
queue.add(multipartAPI);
答案 0 :(得分:0)
定义显示上传进度的进度条
fclose()
将文件上传到服务器
free()
显示警告对话框的方法
private ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
开始上传电话
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
// setting progress bar to zero
progressBar.setProgress(0);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
// Making progress bar visible
progressBar.setVisibility(View.VISIBLE);
// updating progress bar value
progressBar.setProgress(progress[0]);
// updating percentage value
txtPercentage.setText(String.valueOf(progress[0]) + "%");
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("PHP FILE_UPLOAD_URL Here");
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(" Your File Path");
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));
// Extra parameters if you want to pass to server
entity.addPart("website",
new StringBody("www.androidhive.info"));
entity.addPart("email", new StringBody("abc@gmail.com"));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
Log.e(TAG, "Response from server: " + result);
// showing the server response in an alert dialog
showAlert(result);
super.onPostExecute(result);
}
}