无法使用retrofit2下载文件

时间:2015-11-30 14:13:05

标签: retrofit okhttp

文件大小约68M

public interface ServiceApi {
    @GET("QQ_7.9.16621.0_setup.1448418488.exe")
    @Streaming
    Call<ResponseBody> getFile();

    @GET("QQ_7.9.16621.0_setup.1448418488.exe")
    @Streaming
    Observable<Response<ResponseBody>> getFileRx();
}

Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(RestConfig.BASE_URL)
                .client(new OkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

我的gradle配置

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.okhttp:okhttp:2.5.0'

compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.16'

当我使用

serviceApi.getFile()。enqueue(new Callback(){.....}

运行良好;

但是当我使用

serviceApi.getFileRx()
                   .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Response<ResponseBody>>() {....}

它抛出异常,然后失败。

错误日志:

11-30 20:30:54.470 28998-29093/stdying.retrofitdemo W/System.err: java.net.SocketException: Socket closed
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at libcore.io.Posix.recvfromBytes(Native Method)
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at libcore.io.Posix.recvfrom(Posix.java:137)
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
11-30 20:30:54.473 28998-29093/stdying.retrofitdemo W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at okio.Okio$2.read(Okio.java:139)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at okio.RealBufferedSource.read(RealBufferedSource.java:50)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at okio.RealBufferedSource.read(RealBufferedSource.java:50)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at okio.ForwardingSource.read(ForwardingSource.java:35)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at retrofit.OkHttpCall$ExceptionCatchingRequestBody$1.read(OkHttpCall.java:220)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at java.io.InputStream.read(InputStream.java:163)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at stdying.retrofitdemo.MainActivity$2$1.run(MainActivity.java:160)
11-30 20:30:54.477 28998-29093/stdying.retrofitdemo W/System.err:     at java.lang.Thread.run(Thread.java:856)

我使用错误?

0 个答案:

没有答案