在分段上传请求中实现 ProgressDialog

编程入门 行业动态 更新时间:2024-10-15 04:19:38
本文介绍了在分段上传请求中实现 ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用以下方法将图像从 Android 上传到服务器.

I am using following method to upload an image from Android to server.

public void uploadMultipart() { //getting name for the image String name = editText.getText().toString().trim(); //getting the actual path of the image String path = getPath(filePath); progress = ProgressDialog.show(this, "Subiendo imagen", "Por favor, espere...", true); //Uploading code try { String uploadId = UUID.randomUUID().toString(); //Creating a multi part request new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL) .addFileToUpload(path, "image") //Adding file .addParameter("name", name) //Adding text parameter to the request .setNotificationConfig(new UploadNotificationConfig()) .setMaxRetries(2) .startUpload(); //Starting the upload } catch (Exception exc) { Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show(); } }

现在我想实现一个 ProgressDialog,它应该在上传完成时被关闭.我不知道多部分请求何时结束.

Now I want to implement a ProgressDialog that should be dismissed when the upload finishes. I don´t know when the multi part request ends.

谢谢

推荐答案

有这个类:

public class SingleUploadBroadcastReceiver extends UploadServiceBroadcastReceiver { public interface Delegate { void onProgress(int progress); void onProgress(long uploadedBytes, long totalBytes); void onError(Exception exception); void onCompleted(int serverResponseCode, byte[] serverResponseBody); void onCancelled(); } private String mUploadID; private Delegate mDelegate; public void setUploadID(String uploadID) { mUploadID = uploadID; } public void setDelegate(Delegate delegate) { mDelegate = delegate; } @Override public void onProgress(String uploadId, int progress) { if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onProgress(progress); } } @Override public void onProgress(String uploadId, long uploadedBytes, long totalBytes) { if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onProgress(uploadedBytes, totalBytes); } } @Override public void onError(String uploadId, Exception exception) { if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onError(exception); } } @Override public void onCompleted(String uploadId, int serverResponseCode, byte[] serverResponseBody) { if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onCompleted(serverResponseCode, serverResponseBody); } } @Override public void onCancelled(String uploadId) { if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onCancelled(); } } }

然后在你的活动中:

public class YourActivity extends Activity implements SingleUploadBroadcastReceiver.Delegate { private static final String TAG = "AndroidUploadService"; private final SingleUploadBroadcastReceiver uploadReceiver = new SingleUploadBroadcastReceiver(); @Override protected void onResume() { super.onResume(); uploadReceiver.register(this); } @Override protected void onPause() { super.onPause(); uploadReceiver.unregister(this); } public void uploadMultipart(final Context context) { try { String uploadId = UUID.randomUUID().toString(); uploadReceiver.setDelegate(this); uploadReceiver.setUploadID(uploadId); new MultipartUploadRequest(context, uploadId, "upload.server/path") .addFileToUpload("/absolute/path/to/your/file", "your-param-name") .setNotificationConfig(new UploadNotificationConfig()) .setMaxRetries(2) .startUpload(); } catch (Exception exc) { Log.e(TAG, exc.getMessage(), exc); } } @Override public void onProgress(int progress) { //your implementation } @Override public void onProgress(long uploadedBytes, long totalBytes) { //your implementation } @Override public void onError(Exception exception) { //your implementation } @Override public void onCompleted(int serverResponseCode, byte[] serverResponseBody) { //your implementation } @Override public void onCancelled() { //your implementation } }

现在您将在上传成功/不成功时触发适当的回调.

Now you'll have appropriate callbacks fired upon successful/unsuccessful upload.

来源

更多推荐

在分段上传请求中实现 ProgressDialog

本文发布于:2023-11-26 11:25:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   ProgressDialog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!