Retrofit2 请求导致 SocketTimeoutException:

huangapple go评论57阅读模式
英文:

Retrofit2 request gives SocketTimeoutException:

问题

当我尝试在Android中使用Retrofit2请求服务时,服务器响应给出以下错误,异常:连接超时java.net.SocketTimeoutException: 连接超时 at 为什么我在发送请求时会收到Socket超时异常?

请求服务:http://182.20.1.14:2020/api/MainAppExternal/UpdateFcmId

private Retrofit retrofit(String url, boolean addLogInterceptor, int timeout) {
    return new Retrofit.Builder()
            .baseUrl(url).client(getOkHttpClient(addLogInterceptor, timeout))
            .addConverterFactory(GsonConverterFactory.create(LocalGsonBuilder.getInstance().getGson()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
}

private OkHttpClient getOkHttpClient(boolean addLogInterceptor, int timeout) {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
            .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);

    setupInterceptor(httpClient, addLogInterceptor);
    setupTimeout(httpClient, timeout);

    return httpClient.build();
}

private void setupInterceptor(OkHttpClient.Builder httpClient, boolean addLogInterceptor) {
    if (addLogInterceptor) {
        httpClient.addInterceptor(new ConnectionInterceptor());
    }
}

异常:连接超时java.net.SocketTimeoutException: 连接超时 at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:341) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) at java.net.Socket.connect(Socket.java:650) at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223) at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at com.dgpays.mpos.common.common.ConnectionInterceptor.intercept(ConnectionInterceptor.java:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) at okhttp3.RealCall.execute(RealCall.java:69) at retrofit2.OkHttpCall.execute(OkHttpCall.java:180) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41) at io.reactivex.Observable.subscribe(Observable.java:12267) at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34) at io.reactivex.Observable.subscribe(Observable.java:12267) at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96) at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)

英文:

When I try request service to use Retrofit2 in android, server response gives below error, exception : connect timed outjava.net.SocketTimeoutException: connect timed out at why I got socketTimeout exception in send Request?

request service: http://182.20.1.14:2020/api/MainAppExternal/UpdateFcmId

private Retrofit retrofit(String url, boolean addLogInterceptor, int timeout) {
    return new Retrofit.Builder()
            .baseUrl(url).client(getOkHttpClient(addLogInterceptor, timeout))
            .addConverterFactory(GsonConverterFactory.create(LocalGsonBuilder.getInstance().getGson()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
}

private OkHttpClient getOkHttpClient(boolean addLogInterceptor, int timeout) {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
            .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);

    setupInterceptor(httpClient, addLogInterceptor);
    setupTimeout(httpClient, timeout);

    return httpClient.build();
}

private void setupInterceptor(OkHttpClient.Builder httpClient, boolean addLogInterceptor) {
    if (addLogInterceptor) {
        httpClient.addInterceptor(new ConnectionInterceptor());
    }
}

exception : connect timed outjava.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:341) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) at java.net.Socket.connect(Socket.java:650) at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223) at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at com.dgpays.mpos.common.common.ConnectionInterceptor.intercept(ConnectionInterceptor.java:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) at okhttp3.RealCall.execute(RealCall.java:69) at retrofit2.OkHttpCall.execute(OkHttpCall.java:180) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41) at io.reactivex.Observable.subscribe(Observable.java:12267) at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34) at io.reactivex.Observable.subscribe(Observable.java:12267) at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96) at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)

答案1

得分: 2

Translate the code part only:

在代码中添加 getOkHttpClient 函数,如下所示。

private OkHttpClient getOkHttpClient(boolean addLogInterceptor, int timeout) {
    OkHttpClient httpClient = new OkHttpClient.Builder()
            .connectTimeout(timeout, TimeUnit.SECONDS)
            .readTimeout(timeout, TimeUnit.SECONDS)
            .build();
    setupInterceptor(httpClient, addLogInterceptor);
    return httpClient;
}
英文:

Add getOkHttpClient function like this.

private OkHttpClient getOkHttpClient(boolean addLogInterceptor, int timeout) {
    OkHttpClient httpClient = new OkHttpClient.Builder().
            connectTimeout(timeout, TimeUnit.SECONDS).readTimeout(timeout, TimeUnit.SECONDS).build();
    setupInterceptor(httpClient, addLogInterceptor);
    return httpClient.build();
}

huangapple
  • 本文由 发表于 2023年6月6日 17:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定