尝试在Retrofit的下载进度中显示@Get中的@Path

时间:2016-05-11 06:16:14

标签: java android retrofit2

尝试在Retrofit的下载进度中显示@Get中的@Path

我想使用ProgressResponseBody

在进度更新期间显示已传递的@Path

这是我的ProgressListener

public interface ProgressListener  {
    void update(long bytesRead, long contentLength, boolean done , String param);
}

这是我的ProgressResponseBody

public class ProgressResponseBody extends ResponseBody {

    private final Response response;
    private final ResponseBody responseBody;
    private final ProgressListener progressListener;
    private BufferedSource bufferedSource;

    public ProgressResponseBody(Response response, ProgressListener progressListener) {
        this.response = response;
        this.responseBody = response.body();
        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;
                progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1 , "Dont know how to get it here");
                return bytesRead;
            }
        };
    }
}

这是我的Retrofit Library

@Module(
        library = true,
        complete = false
)
public class RetrofitLibrary {


    @Provides
    @Singleton
    @Named(Constants.MAP_HTTP_CONSTANTS)
    public Retrofit provideRetrofitForHttp(@ForApplication Context context , @ForEventBus EventBus mBus){
        final OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Response originalResponse = chain.proceed(chain.request());
                        return originalResponse.newBuilder()
                                .body(new ProgressResponseBody(originalResponse, new ProgressListener(){
                                    @Override
                                    public void update(long bytesRead, long contentLength, boolean done, String param) {
                                        // sending data using EventBus here
                                    }
                                }))
                                .build();
                    }
                })
                .build();


        return new Retrofit.Builder()
                .baseUrl("URL here")
                .client(client)
                .build();
    }

}

这是我的DownloadService

  @GET("/someurl/{map_name}.mbtiles")
    @Streaming
    Call<ResponseBody> downloadMap(@Path("map_name") String map_name );

现在我想知道这是否会起作用,我不知道如何获得@Path专门map_name。

0 个答案:

没有答案