【问题标题】:Android Retrofit - onProgressUpdate for showing Progress NotificationAndroid Retrofit - onProgressUpdate 用于显示进度通知
【发布时间】:2014-06-14 10:55:49
【问题描述】:

我目前正在使用 Square 的 Retrofit 进行 Android 网络通信。有没有办法在创建进度通知的任务期间获取进度,类似于 Facebook 在上传图片时使用的方法?

用例是在不压缩或缩放的情况下加载具有完整图像质量的图像。

我看到了异步任务是如何实现的,但这会破坏使用 Retrofit 的目的。然而,这可能是我必须采取的路线。

【问题讨论】:

  • 你能详细说明用例吗?大多数简单的改造请求都很短,不需要进度指示器,但我猜你正在使用 PUT 或 POST 来上传图片之类的文件?
  • 正是@Hober,你会建议不要使用改造来上传图片吗?如果有,您有什么建议吗?
  • 我前段时间实现了这个,但找不到解决方案,所以我做了一个不确定的进度条。
  • @NujnaH 准确的说你的意思是你的意思是你用改造实现了发送图片,并且只是创建了一个不确定的进度条,然后在 Success 期间就完成了? (这基本上也是我现在正在做的)
  • 但这个问题非常有效。上传大量数据时是否可以显示改造进度?不确定的进度条是一种解决方法。

标签: android android-asynctask retrofit


【解决方案1】:

此答案适用于 Retrofit 1。适用于与 Retrofit 2 see this answer 兼容的解决方案。


我遇到了同样的问题,终于成功了。我以前使用过spring lib,下面显示的内容适用于Spring,但由于我在将它用于InputStream时犯了一个错误,所以不一致。我将所有 API 移动到使用改造,上传是列表中的最后一个,我只是重写 TypedFile writeTo() 来更新我读取到 OutputStream 的字节。也许这可以改进,但正如我所说,我是在使用 Spring 时做到的,所以我只是重用了它。 这是上传的代码,它在我的应用程序上为我工作,如果你想要下载反馈,那么你可以使用@Streaming 并阅读 inputStream。

进度监听器

public interface ProgressListener {
 void transferred(long num);
}

CountingTypedFile

public class CountingTypedFile extends TypedFile {

 private static final int BUFFER_SIZE = 4096;

 private final ProgressListener listener;

 public CountingTypedFile(String mimeType, File file, ProgressListener listener) {
    super(mimeType, file);
    this.listener = listener;
 }

 @Override public void writeTo(OutputStream out) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    FileInputStream in = new FileInputStream(super.file());
    long total = 0;
    try {
        int read;
        while ((read = in.read(buffer)) != -1) {
            total += read;
            this.listener.transferred(total);
            out.write(buffer, 0, read);
        }
    } finally {
        in.close();
    }
 }
}

MyApiService

public interface MyApiService {
 @Multipart
 @POST("/files")
 ApiResult uploadFile(@Part("file") TypedFile resource, @Query("path") String path);
}

发送文件任务

private class SendFileTask extends AsyncTask<String, Integer, ApiResult> {
    private ProgressListener listener;
    private String filePath;
    private FileType fileType;

    public SendFileTask(String filePath, FileType fileType) {
        this.filePath = filePath;
        this.fileType = fileType;
    }

    @Override
    protected ApiResult doInBackground(String... params) {
        File file = new File(filePath);
        totalSize = file.length();
        Logger.d("Upload FileSize[%d]", totalSize);
        listener = new ProgressListener() {
            @Override
            public void transferred(long num) {
                publishProgress((int) ((num / (float) totalSize) * 100));
            }
        };
        String _fileType = FileType.VIDEO.equals(fileType) ? "video/mp4" : (FileType.IMAGE.equals(fileType) ? "image/jpeg" : "*/*");
        return MyRestAdapter.getService().uploadFile(new CountingTypedFile(_fileType, file, listener), "/Mobile Uploads");
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        Logger.d(String.format("progress[%d]", values[0]));
        //do something with values[0], its the percentage so you can easily do
        //progressBar.setProgress(values[0]);
    }
}

CountingTypedFile 只是 TypedFile 的副本,但包括 ProgressListener。

【讨论】:

  • 你。是。国王。这是伟大的工作,戴维。特别是在这方面几乎没有文档。顺便说一句,我将CountingTypedFile 重命名为TypedFileWithProgressListener
  • 我很高兴能帮到人!在可能的情况下尝试迁移到改造 2.x,它们已经改进,现在这是框架的一部分。
【解决方案2】:

如果您想获取最大值以便在 ProgressDialog、Notification 等上显示它​​。

进度监听器

public interface ProgressListener {
    void transferred(long num, long max);
}

CountingTypedFile

public class CountingTypedFile extends TypedFile {

    private static final int BUFFER_SIZE = 4096;

    private final ProgressListener listener;

    public CountingTypedFile(String mimeType, File file, ProgressListener listener) {
        super(mimeType, file);
        this.listener = listener;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        FileInputStream in = new FileInputStream(super.file());
        long total = 0;
        try {
            int read;
            while ((read = in.read(buffer)) != -1) {
                total += read;
                this.listener.transferred(total, super.file().length());
                out.write(buffer, 0, read);
            }
        } finally {
            in.close();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2013-04-09
    • 2017-06-13
    相关资源
    最近更新 更多