square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
42.99k stars 7.3k forks source link

There is a problem when upload a big file #2271

Closed summer-zhoujie closed 7 years ago

summer-zhoujie commented 7 years ago

Interface

    @Multipart
    @POST("http://sdkconfig.reedoun.com/upload/?key=94b8862c-761d-493c-8669-3d96ea5cefea_866929022179744")
    Observable<BaseResultEntity<Object>> uploadFile(@Part MultipartBody.Part file);

invoke

 ServiceManager.getInstance().getUploadApiImpl().UploadFile(file,
                    file.getName()).compose(this.bindUntilEvent())
            .subscribe(new ProgressSubscriber(Main2Activity.this) {
                @Override
                protected void next(Object o) {
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    ToastUtils.showShortToast("完成");
                }

                @Override
                public void onCancelProgress() {
                    super.onCancelProgress();
                    ToastUtils.showShortToast("取消");
                }

                @Override
                protected void _onError(Throwable e) {
                    ToastUtils.showShortToast("错误");
                }
            });

public Observable UploadFile(File file, String fileName) {

        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                                                     file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file_name",
                                                                    TextUtils.isEmpty(
                                                                            fileName) ? file.getName() : fileName,
                                                                    requestBody);
        Observable observable = api.uploadFile(part)
                /* 失败后的retry配置 */
                .retryWhen(new RetryWhenNetworkException());

        return observable;
    }

log

D/[<NET> at log(ServiceManager$1.java:35)]: --> POST http://sdkconfig.reedoun.com/upload/?key=94b8862c-761d-493c-8669-3d96ea5cefea_866929022179744 http/1.1 (248693640-byte body)
D/[<NET> at log(ServiceManager$1.java:35)]: <-- HTTP FAILED: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
W/System.err: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:515)
W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
W/System.err:     at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
W/System.err:     at okio.Okio$1.write(Okio.java:78)
W/System.err:     at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
W/System.err:     at okio.RealBufferedSink.write(RealBufferedSink.java:41)
W/System.err:     at okhttp3.internal.http1.Http1Codec$FixedLengthSink.write(Http1Codec.java:286)
W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
W/System.err:     at okio.RealBufferedSink.writeAll(RealBufferedSink.java:99)
W/System.err:     at okhttp3.RequestBody$3.writeTo(RequestBody.java:118)
W/System.err:     at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171)
W/System.err:     at okhttp3.MultipartBody.writeTo(MultipartBody.java:113)
W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:59)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
W/System.err:     at okhttp3.RealCall.execute(RealCall.java:63)
W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
W/System.err:     at rx.internal.producers.ProducerArbiter.setProducer(ProducerArbiter.java:126)
W/System.err:     at rx.internal.operators.OnSubscribeRedo$2$1.setProducer(OnSubscribeRedo.java:272)
W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:205)
W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
W/System.err:     at rx.Observable.unsafeSubscribe(Observable.java:8460)
W/System.err:     at rx.internal.operators.OnSubscribeRedo$2.call(OnSubscribeRedo.java:278)
W/System.err:     at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:76)
W/System.err:     at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:55)
W/System.err:     at rx.internal.operators.OnSubscribeRedo$5.request(OnSubscribeRedo.java:366)
W/System.err:     at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:209)
W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:205)
W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:205)
W/System.err:     at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:358)
W/System.err:     at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:55)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
W/System.err:     at rx.Observable.unsafeSubscribe(Observable.java:8460)
W/System.err:     at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
W/System.err:     at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:222)
W/System.err:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: android.system.ErrnoException: sendto failed: ECONNRESET (Connection reset by peer)
W/System.err:     at libcore.io.Posix.sendtoBytes(Native Method)
W/System.err:     at libcore.io.Posix.sendto(Posix.java:206)
W/System.err:     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:513)
W/System.err:   ... 65 more

what happened? I can not fix it myself

xesam commented 7 years ago

这是服务器的问题。

summer-zhoujie commented 7 years ago

@xesam 还请明示,非常感谢!!!

JakeWharton commented 7 years ago

The server you're sending the data to timed out and closed the connection. This isn't anything wrong with Retrofit. You either need to compress the data on the client side more or adjust the timeouts on the server side.