首页 > 关于asynchttp网络协议的问题

关于asynchttp网络协议的问题

相信大部分开发者都用过asynchttp框架,关于新版本的网络连接问题,一直有一个疑问,就是这个封装好的框架的中,如何获取上传文件或者下载文件的进度?


菜鸟,也没有用过asynchttp,但是用okhttp搞过传递下载进度,也许有点参考价值?

FileOutputStream out = new FileOutputStream(fileName);
InputStream in = response.body().byteStream();//从reponse中获得输入流
byte[] buffs = new byte[20460];//写入缓冲区
int count;//从输入流写入缓冲区的大小
int size = Integer.valueOf(length);//待下载文件总大小
long per;//下载百分比
long total = 0;//累计写入文件大小
long time = SystemClock.elapsedRealtime();
while ((count = in.read(buffs)) != -1) {
    out.write(buffs, 0, count);
    total += count;
    per = 100 * total / size;
    if (callback != null && (SystemClock.elapsedRealtime() - time) > 500) {
        callback.getDownloadPercentage((int) per);//回调方法,每隔500毫秒更新下载百分比一次,千万不要更新太频繁,主线程会卡住的
        Log.d("per", per + "");
        time = SystemClock.elapsedRealtime();
    }
}

求大神解答一下下

【热门文章】
【热门文章】