我是新来的改造.我搜索过但没有找到简单的答案.我想知道如何在通知栏中显示下载进度或至少显示进度对话框,该对话框指定进程的百分比和下载文件的大小.
这是我的代码:
public interface ServerAPI { @GET Call<ResponseBody> downlload(@Url String fileUrl); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.43.135/retro/") .addConverterFactory(GsonConverterFactory.create()) .build();}public voID download(){ ServerAPI API = ServerAPI.retrofit.create(ServerAPI.class); API.downlload("https://www.Google.com/images/branding/Googlelogo/2x/Googlelogo_color_120x44dp.png").enqueue(new Callback<ResponseBody>() { @OverrIDe public voID onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { file path = Environment.getExternalStorageDirectory(); file file = new file(path, "file_name.jpg"); fileOutputStream fileOutputStream = new fileOutputStream(file); IoUtils.write(response.body().bytes(), fileOutputStream); } catch (Exception ex){ } } @OverrIDe public voID onFailure(Call<ResponseBody> call, Throwable t) { } });}
如果可以,请指导我.
谢谢
解决方法:
您需要创建一个特定的Okhttp客户端,它将拦截网络请求并发送更新.此客户端应仅用于下载.
首先,您将需要一个接口,如下所示:
public interface OnAttachmentDownloadListener { voID onAttachmentDownloadedSuccess(); voID onAttachmentDownloadedError(); voID onAttachmentDownloadedFinished(); voID onAttachmentDownloadUpdate(int percent);}
您的下载调用应该返回一个ResponseBody,我们将从中扩展以获得下载进度.
private static class ProgressResponseBody extends ResponseBody { private final ResponseBody responseBody; private final OnAttachmentDownloadListener progressListener; private BufferedSource bufferedSource; public ProgressResponseBody(ResponseBody responseBody, OnAttachmentDownloadListener 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); totalBytesRead += bytesRead != -1 ? bytesRead : 0; float percent = bytesRead == -1 ? 100f : (((float)totalBytesRead / (float) responseBody.contentLength()) * 100); if(progressListener != null) progressListener.onAttachmentDownloadUpdate((int)percent); return bytesRead; } }; }}
然后你需要像这样创建你的OkhttpClIEnt
public OkhttpClIEnt.Builder getokhttpDownloadClIEntBuilder(OnAttachmentDownloadListener progressListener) { OkhttpClIEnt.Builder httpClIEntBuilder = new OkhttpClIEnt.Builder(); // You might want to increase the timeout httpClIEntBuilder.connectTimeout(20, TimeUnit.SECONDS); httpClIEntBuilder.writeTimeout(0, TimeUnit.SECONDS); httpClIEntBuilder.readTimeout(5, TimeUnit.MINUTES); httpClIEntBuilder.addInterceptor(new Interceptor() { @OverrIDe public Response intercept(Chain chain) throws IOException { if(progressListener == null) return chain.proceed(chain.request()); Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)) .build(); } }); return httpClIEntBuilder;}
最后,您只需通过传递新的Okhttp客户端,以不同的方式创建您的Retrofit客户端.根据您的代码,您可以使用以下内容:
public Retrofit getDownloadRetrofit(OnAttachmentDownloadListener Listener) { return new Retrofit.Builder() .baseUrl("http://192.168.43.135/retro/") .addConverterFactory(GsonConverterFactory.create()) .clIEnt(getokhttpDownloadClIEntBuilder(Listener).build()) .build();}
您的听众将处理您的通知的创建或您想要的任何其他内容.
总结以上是内存溢出为你收集整理的android改造下载进度全部内容,希望文章能够帮你解决android改造下载进度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)