使用排球时如何保持进度条状态? [英] How to maintain progress bar state when using volley?

查看:66
本文介绍了使用排球时如何保持进度条状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将AysncTask与片段Callback一起使用时,维护进度条状态很容易,但是我应该如何用凌空抽射来实现呢?我不能使用AsyncTask,因为它已经过时并且凌空变得越来越快.任何帮助或提示将不胜感激.

It is easy to maintain progress bar state when i use AysncTask with fragments Callback but how should i achieve it with volley? I can;t use AsyncTask because it is outdated and volley is better and faster. Any Help or Hint will be grateful.

我正在使用Google的齐射来发布和获取请求

I am using google's volley to Post and Get Requests

推荐答案

我认为这里存在误解.

首先,Volley比AsyncTask快.

First off, Volley faster than AsyncTask.

这是在比较苹果和橘子.它们都使用线程.Volley线程并不比异步任务中的线程快.队列是分开的,但仅此而已.在API 11和更高的级别,允许您为 AsyncTask 实例使用自己的线程池.

This is comparing apples and oranges. They both use threads. Volley threads are not any faster than the threads in async task. The queues are separate but that is about it. In API 11 & higher you are allowed to use your own threadpool for AsyncTask instances.

第二,定义更好.

  • Volley旨在向服务器发送大量轻负载(GET/POST),并获得非常快速的响应.然后,呼叫者可以使用这些响应.

  • Volley is designed for sending a lot of light payloads (GET/POST) to a server and getting back pretty quick responses. These responses can then be used by the caller.

AsyncTask 旨在从UI线程完成给定任务,并提供有关该任务状态的各种回调.

AsyncTask is designed to complete a given task off the UI thread and provide various callbacks as to the state of that task.

对于您的 ProgressBar ,我假设您正在尝试确定正在执行的请求的进度.在Volley世界中,由于预计这些状态很小,因此您几乎拥有3个州.未启动,正在执行(还包含启动解析)和已完成(包含成功,错误和已取消等).如您所知,使用 publishProgress 时,使用 AsyncTask 进行 onProgress 的回调.因此,您的实例可以定义要发送的任何东西,以指示进度.

For your ProgressBar I am assuming you are either trying to determine the progress of a request that is being executed. In the Volley world since these are expected to be tiny, you have pretty much 3 states. Not Started, Executing(also contains start parsing) and Done (comprised of success, error and cancelled and such). As you know with AsyncTask there is a callback for onProgress when using publishProgress. So your instance can define anything it wants to send through as an indication of progress.

如果您的有效负载很大并且需要花费一些时间才能传输到服务器,则Volley可能不合适.Volley并没有做得很好,甚至在向服务器和从服务器发送大型有效负载方面也做得并不出色.原因是这不是它的意图.就像它要求所有有效载荷一样,上载和接收都可以完全容纳在内存中.因此,如果您遇到了几个凌空请求,并且每个请求都有1MB的有效负载和1MB的响应,那么您会很快发现这一点.

If your payload is big and will take time to transfer to the server, Volley may not be appropriate. Volley doesn't do a great job or even try to do a great job of sending large payloads to and from a server. The reason is that this just isn't what it is meant for. Like it requires that all payloads, upload and receive can fit in memory entirely. So If you have a few volley requests going all over, and each one with like a 1MB payload and a 1MB response, you could see this adding up pretty quickly.

Volley是一个很棒的库,但是请考虑建议使用它.请阅读代码的文档和实现以获取更多信息.

Volley is great library but consider what it is recommended to be used for. Read the documentation and implementation of the code for more info.

如果您要做的事情需要花费相当长的时间,我将编写一个特定的请求类型,该类型向/从/向其发送内容.这样,您可以知道请求还剩下多少工作.我假设您使用发送和接收的字节作为进度的度量.

If you are doing something that is going to take a rather long time, I would write a specific request type that sends and streams content to and from. That way you can tell how much work is left with the request. I am assuming you are using bytes sent and receive as the measure for progress.

这篇关于使用排球时如何保持进度条状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆