如何在不使用kotlin中的Asynctask的情况下调用Api的百分比进度?

目的 :我使用retrofit库进行api调用时使用progress dialog 。 为此,我使用下面的代码:

 private var progressDialog: ProgressDialog? = null @JvmStatic fun showProgress(message: String?, context: Context?, cancellable: Boolean) { if (context == null) return if (checkProgressOpen()) return progressDialog = ProgressDialog(context) (progressDialog as ProgressDialog).setProgressStyle(ProgressDialog.STYLE_SPINNER) (progressDialog as ProgressDialog).setMessage(message ?: "Please wait...") (progressDialog as ProgressDialog).setCancelable(cancellable) try { (progressDialog as ProgressDialog).show() } catch (e: Exception) { // catch exception for activity paused and dialog is going to be // show. } } @JvmStatic fun checkProgressOpen(): Boolean { if (progressDialog != null && (progressDialog as ProgressDialog).isShowing()) return true else return false } @JvmStatic fun cancelProgress() { if (checkProgressOpen()) { try { (progressDialog as ProgressDialog).dismiss() (progressDialog as ProgressDialog).cancel() } catch (e: Exception) { e.printStackTrace() } progressDialog = null } } 

这段代码工作正常。 它显示progress dialog ,但是我现在想要的是以百分比显示progress dialog

就像api call开始dialog应该从百分比0开始,它应该在某个时间间隔更新百分比,而当完成Api call dialog应该显示百分比100。

我已经完成了对此的研发,但是到处都是,使用asynctask和它的方法OnProgressUpdate ,但我不想使用asynctask

任何人都可以帮助我吗? 提前致谢 !!

您需要创建自定义RequestBody并在Retrofit RequestBody其用作RequestBody 。 这里是ProgressRequestBody示例

这将为您提供porgress的更新。 你可以根据你的需要定制这个。

 open class ProgressRequestBody(private val mFile: File) : RequestBody() { private val ignoreFirstNumberOfWriteToCalls: Int = 0 private var numWriteToCalls = 0 private val getProgressSubject: PublishSubject> = PublishSubject.create>() fun getProgressSubject(): Observable> { return getProgressSubject } override fun contentType(): MediaType? { return MediaType.parse("application/pdf") } @Throws(IOException::class) override fun contentLength(): Long { return mFile.length() } @Throws(IOException::class) override fun writeTo(sink: BufferedSink) { numWriteToCalls++ val fileLength = mFile.length() val buffer = ByteArray(DEFAULT_BUFFER_SIZE) val `in` = FileInputStream(mFile) var uploaded: Long = 0 var lastProgressPercentUpdate = 0f `in`.use { `in` -> var read: Int read = `in`.read(buffer) while (read != -1) { uploaded += read.toLong() sink.write(buffer, 0, read) read = `in`.read(buffer) //GLOBAL_PROGRESS.onNext(uploaded) // when using HttpLoggingInterceptor it calls writeTo and passes data into a local buffer just for logging purposes. // the second ic_call to write to is the progress we actually want to track if (numWriteToCalls > ignoreFirstNumberOfWriteToCalls) { val progress = (uploaded.toFloat() / fileLength.toFloat()) * 100f //prevent publishing too many updates, which slows upload, by checking if the upload has progressed by at least 1 percent if (progress - lastProgressPercentUpdate > 1 || progress == 100f) { // publish progress getProgressSubject.onNext(Pair(progress, mFile)) lastProgressPercentUpdate = progress } } } } } companion object { private val DEFAULT_BUFFER_SIZE = 2048 } }