一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 基于Retrofit+Rxjava實現帶進度顯示的下載文件

基于Retrofit+Rxjava實現帶進度顯示的下載文件

2021-04-27 14:12JokAr- Java教程

這篇文章主要為大家詳細介紹了基于Retrofit+Rxjava實現帶進度顯示的下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了retrofit rxjava實現下載文件的具體代碼,供大家參考,具體內容如下

本文采用 :retrofit + rxjava

1.引入:

?
1
2
3
4
5
6
7
8
//rxjava
 compile 'io.reactivex:rxjava:latest.release'
 compile 'io.reactivex:rxandroid:latest.release'
 //network - squareup
 compile 'com.squareup.retrofit2:retrofit:latest.release'
 compile 'com.squareup.retrofit2:adapter-rxjava:latest.release'
 compile 'com.squareup.okhttp3:okhttp:latest.release'
 compile 'com.squareup.okhttp3:logging-interceptor:latest.release'

2.增加下載進度監聽:

?
1
2
3
public interface downloadprogresslistener {
 void update(long bytesread, long contentlength, boolean done);
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class downloadprogressresponsebody extends responsebody {
 
 private responsebody responsebody;
 private downloadprogresslistener progresslistener;
 private bufferedsource bufferedsource;
 
 public downloadprogressresponsebody(responsebody responsebody,
          downloadprogresslistener progresslistener) {
  this.responsebody = responsebody;
  this.progresslistener = progresslistener;
 }
 
 @override
 public mediatype contenttype() {
  return responsebody.contenttype();
 }
 
 @override
 public long contentlength() {
  return responsebody.contentlength();
 }
 
 @override
 public bufferedsource source() {
  if (bufferedsource == null) {
   bufferedsource = okio.buffer(source(responsebody.source()));
  }
  return bufferedsource;
 }
 
 private source source(source source) {
  return new forwardingsource(source) {
   long totalbytesread = 0l;
 
   @override
   public long read(buffer sink, long bytecount) throws ioexception {
    long bytesread = super.read(sink, bytecount);
    // read() returns the number of bytes read, or -1 if this source is exhausted.
    totalbytesread += bytesread != -1 ? bytesread : 0;
 
    if (null != progresslistener) {
     progresslistener.update(totalbytesread, responsebody.contentlength(), bytesread == -1);
    }
    return bytesread;
   }
  };
 
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class downloadprogressinterceptor implements interceptor {
 
 private downloadprogresslistener listener;
 
 public downloadprogressinterceptor(downloadprogresslistener listener) {
  this.listener = listener;
 }
 
 @override
 public response intercept(chain chain) throws ioexception {
  response originalresponse = chain.proceed(chain.request());
 
  return originalresponse.newbuilder()
    .body(new downloadprogressresponsebody(originalresponse.body(), listener))
    .build();
 }
}

3.創建下載進度的元素類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class download implements parcelable {
 
 private int progress;
 private long currentfilesize;
 private long totalfilesize;
 
 public int getprogress() {
  return progress;
 }
 
 public void setprogress(int progress) {
  this.progress = progress;
 }
 
 public long getcurrentfilesize() {
  return currentfilesize;
 }
 
 public void setcurrentfilesize(long currentfilesize) {
  this.currentfilesize = currentfilesize;
 }
 
 public long gettotalfilesize() {
  return totalfilesize;
 }
 
 public void settotalfilesize(long totalfilesize) {
  this.totalfilesize = totalfilesize;
 }
 
 @override
 public int describecontents() {
  return 0;
 }
 
 @override
 public void writetoparcel(parcel dest, int flags) {
  dest.writeint(this.progress);
  dest.writelong(this.currentfilesize);
  dest.writelong(this.totalfilesize);
 }
 
 public download() {
 }
 
 protected download(parcel in) {
  this.progress = in.readint();
  this.currentfilesize = in.readlong();
  this.totalfilesize = in.readlong();
 }
 
 public static final parcelable.creator<download> creator = new parcelable.creator<download>() {
  @override
  public download createfromparcel(parcel source) {
   return new download(source);
  }
 
  @override
  public download[] newarray(int size) {
   return new download[size];
  }
 };
}

4.下載文件網絡類:

?
1
2
3
4
5
6
public interface downloadservice {
 
 @streaming
 @get
 observable<responsebody> download(@url string url);
}

注:這里@url是傳入完整的的下載url;不用截取

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class downloadapi {
 private static final string tag = "downloadapi";
 private static final int default_timeout = 15;
 public retrofit retrofit;
 
 
 public downloadapi(string url, downloadprogresslistener listener) {
 
  downloadprogressinterceptor interceptor = new downloadprogressinterceptor(listener);
 
  okhttpclient client = new okhttpclient.builder()
    .addinterceptor(interceptor)
    .retryonconnectionfailure(true)
    .connecttimeout(default_timeout, timeunit.seconds)
    .build();
 
 
  retrofit = new retrofit.builder()
    .baseurl(url)
    .client(client)
    .addcalladapterfactory(rxjavacalladapterfactory.create())
    .build();
 }
 
 public void downloadapk(@nonnull string url, final file file, subscriber subscriber) {
  log.d(tag, "downloadapk: " + url);
 
  retrofit.create(downloadservice.class)
    .download(url)
    .subscribeon(schedulers.io())
    .unsubscribeon(schedulers.io())
    .map(new func1<responsebody, inputstream>() {
     @override
     public inputstream call(responsebody responsebody) {
      return responsebody.bytestream();
     }
    })
    .observeon(schedulers.computation())
    .doonnext(new action1<inputstream>() {
     @override
     public void call(inputstream inputstream) {
      try {
       fileutils.writefile(inputstream, file);
      } catch (ioexception e) {
       e.printstacktrace();
       throw new customizeexception(e.getmessage(), e);
      }
     }
    })
    .observeon(androidschedulers.mainthread())
    .subscribe(subscriber);
 }
 
 
}

然后就是調用了:

該網絡是在service里完成的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class downloadservice extends intentservice {
 private static final string tag = "downloadservice";
 
 private notificationcompat.builder notificationbuilder;
 private notificationmanager notificationmanager;
 
 
 private string apkurl = "http://download.fir.im/v2/app/install/595c5959959d6901ca0004ac?download_token=1a9dfa8f248b6e45ea46bc5ed96a0a9e&source=update";
 
 public downloadservice() {
  super("downloadservice");
 }
 
 @override
 protected void onhandleintent(intent intent) {
  notificationmanager = (notificationmanager) getsystemservice(context.notification_service);
 
  notificationbuilder = new notificationcompat.builder(this)
    .setsmallicon(r.mipmap.ic_download)
    .setcontenttitle("download")
    .setcontenttext("downloading file")
    .setautocancel(true);
 
  notificationmanager.notify(0, notificationbuilder.build());
 
  download();
 }
 
 private void download() {
  downloadprogresslistener listener = new downloadprogresslistener() {
   @override
   public void update(long bytesread, long contentlength, boolean done) {
    download download = new download();
    download.settotalfilesize(contentlength);
    download.setcurrentfilesize(bytesread);
    int progress = (int) ((bytesread * 100) / contentlength);
    download.setprogress(progress);
 
    sendnotification(download);
   }
  };
  file outputfile = new file(environment.getexternalstoragepublicdirectory
    (environment.directory_downloads), "file.apk");
  string baseurl = stringutils.gethostname(apkurl);
 
  new downloadapi(baseurl, listener).downloadapk(apkurl, outputfile, new subscriber() {
   @override
   public void oncompleted() {
    downloadcompleted();
   }
 
   @override
   public void onerror(throwable e) {
    e.printstacktrace();
    downloadcompleted();
    log.e(tag, "onerror: " + e.getmessage());
   }
 
   @override
   public void onnext(object o) {
 
   }
  });
 }
 
 private void downloadcompleted() {
  download download = new download();
  download.setprogress(100);
  sendintent(download);
 
  notificationmanager.cancel(0);
  notificationbuilder.setprogress(0, 0, false);
  notificationbuilder.setcontenttext("file downloaded");
  notificationmanager.notify(0, notificationbuilder.build());
 }
 
 private void sendnotification(download download) {
 
  sendintent(download);
  notificationbuilder.setprogress(100, download.getprogress(), false);
  notificationbuilder.setcontenttext(
    stringutils.getdatasize(download.getcurrentfilesize()) + "/" +
      stringutils.getdatasize(download.gettotalfilesize()));
  notificationmanager.notify(0, notificationbuilder.build());
 }
 
 private void sendintent(download download) {
 
  intent intent = new intent(mainactivity.message_progress);
  intent.putextra("download", download);
  localbroadcastmanager.getinstance(downloadservice.this).sendbroadcast(intent);
 }
 
 @override
 public void ontaskremoved(intent rootintent) {
  notificationmanager.cancel(0);
 }
}

mainactivity代碼:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class mainactivity extends appcompatactivity {
 
 public static final string message_progress = "message_progress";
 
 private appcompatbutton btn_download;
 private progressbar progress;
 private textview progress_text;
 
 
 private broadcastreceiver broadcastreceiver = new broadcastreceiver() {
  @override
  public void onreceive(context context, intent intent) {
 
   if (intent.getaction().equals(message_progress)) {
 
    download download = intent.getparcelableextra("download");
    progress.setprogress(download.getprogress());
    if (download.getprogress() == 100) {
 
     progress_text.settext("file download complete");
 
    } else {
 
     progress_text.settext(stringutils.getdatasize(download.getcurrentfilesize())
       +"/"+
       stringutils.getdatasize(download.gettotalfilesize()));
 
    }
   }
  }
 };
 
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  btn_download = (appcompatbutton) findviewbyid(r.id.btn_download);
  progress = (progressbar) findviewbyid(r.id.progress);
  progress_text = (textview) findviewbyid(r.id.progress_text);
 
  registerreceiver();
 
  btn_download.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
    intent intent = new intent(mainactivity.this, downloadservice.class);
    startservice(intent);
   }
  });
 }
 
 private void registerreceiver() {
 
  localbroadcastmanager bmanager = localbroadcastmanager.getinstance(this);
  intentfilter intentfilter = new intentfilter();
  intentfilter.addaction(message_progress);
  bmanager.registerreceiver(broadcastreceiver, intentfilter);
 
 }
}

本文源碼:retrofit rxjava實現下載文件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/a1018875550/article/details/51832700

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好湿好紧太硬了我好爽 | 国产永久免费视频m3u8 | 午夜精品久久久久久中宇 | 天天舔天天操天天干 | 四虎永久免费在线观看 | 日本大学生xxxxx69泡妞 | 草草在线免费视频 | 久久久精品3d动漫一区二区三区 | 精品videoss另类日本 | 二次元美女扒开内裤露尿口 | 色老妈| 性色生活片在线观看 | 国产91精选学生在线观看 | 亚洲av欧美在我 | 精品一久久香蕉国产线看观 | 国产成人毛片 | 国产999在线观看 | 美女岳肉太深了使劲 | 天天爽天天干天天操 | 美女脱一光二净的视频 | 国产日韩高清一区二区三区 | 青草青青在线视频 | 国产香蕉久久 | 亚洲av欧美在我 | 99热er| 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 91日本| 放荡女小洁的性日记 | 99这里只有精品视频 | 日本高清在线看免费观看 | 欧美成人三级伦在线观看 | 倩女还魂在线观看完整版免费 | 金发美女与黑人做爰 | 好男人资源免费播放在线观看 | 亚洲成年网站在线777 | 高h全肉动漫在线观看免费 高h辣h双处全是肉军婚 | 男女真实无遮挡xx00动态图软件 | 女人张开腿让男人桶爽 | 国产精品久久久天天影视香蕉 | 国产成人性毛片aaww | 亚洲欧美日韩另类精品一区二区三区 |