2017-02-08 8 views
4

私は改装することが新規です。私は検索しましたが、簡単な答えは見つかりませんでした。どのように通知バーにダウンロードの進行状況を表示するか、少なくともファイルのダウンロードのプロセスとサイズの割合を指定する進行状況ダイアログを表示することができます。できればアンドロイド改造ダウンロードの進捗状況

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) { 
     } 
    }); 
} 

が私を導いてください: はここに私のコードです。 おかげ

答えて

1

あなたが自分でそれを実装する必要があなたはいけない、見hereを取ることができ、背後にある考え方は、要求のコンテンツ長を取ることで、あなたがバッファに書くときだけ

6

あなたの進捗状況を計算します特定の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; 
      } 
     }; 
    } 
} 

次に、あなたが最後にあなただけの新しいOkHttpクライアントを渡すことによって、あなたのレトロフィットクライアント別の方法を作成する必要があり、この

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; 
} 

のようなあなたのOkHttpClientを作成する必要があります。あなたのコードに基づいて、あなたはこのようなものを使用することができます。

public Retrofit getDownloadRetrofit(OnAttachmentDownloadListener listener) { 

    return new Retrofit.Builder() 
       .baseUrl("http://192.168.43.135/retro/") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(getOkHttpDownloadClientBuilder(listener).build()) 
       .build(); 

} 

あなたのリスナーがあなたの通知や他のものは何でもしたいの作成を処理します。

関連する問題