• 在Java中进行api调用时,出现“Exception in thread "main" java.net.SocketTimeoutException: timeout”,“Caused by: java.net.SocketTimeoutException: Read timed out”的错误


    在本地使用 OkHttpClient 创建请求客户端,调试api接口时,出现了表示中所述的错误,先贴出完整错误代码

    Exception in thread "main" java.net.SocketTimeoutException: timeout
        at okio.Okio$4.newTimeoutException(Okio.java:230)
        at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
        at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        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 okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
        at okhttp3.RealCall.execute(RealCall.java:69)
        at com.***.***Api.***(***Api.java:20) at com.***.***Api.main(***Api.java:26) 
    Caused by: java.net.SocketTimeoutException: Read timed out at
    java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:
    116)
    at java.net.SocketInputStream.read(SocketInputStream.java:
    171)
    at java.net.SocketInputStream.read(SocketInputStream.java:
    141)
    at okio.Okio$
    2.read(Okio.java:139) at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
    ...
    21 more

    问题在于api里面处理的时间比较久,而我所创建的 OkHttpClient 客户端并没有给足够的时间取处理,因此需要设置更长的连接和读取时间,延长至50秒,可顺利通过。代码如下所示:

    OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(50000, TimeUnit.MILLISECONDS)
                    .readTimeout(50000, TimeUnit.MILLISECONDS)
                    .build();

    网上有说通过下面代码设置超时时间,但是没有找到set的方法,有知情的小伙伴告知一下吧。

    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(30, TimeUnit.SECONDS);    // socket timeout
  • 相关阅读:
    bzoj4753: [Jsoi2016]最佳团体(分数规划+树形依赖背包)
    bzoj2956: 模积和(数论)
    51nod 1766 树上的最远点对(线段树)
    bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)
    Codeforces Round #441 Div. 2题解
    bzoj4569: [Scoi2016]萌萌哒(ST表+并查集)
    iOS和Android后台机制对比
    UIApplicationDelegate 各方法回调时机
    iOS OC和JS的交互 javaScriptCore方法封装
    iOS应用的执行原理
  • 原文地址:https://www.cnblogs.com/Faquir/p/9268979.html
Copyright © 2020-2023  润新知