How to set connection timeout with OkHttp

后端 未结 11 907
一向
一向 2020-11-27 10:32

I am developing app using OkHttp library and my trouble is I cannot find how to set connection timeout and socket timeout.

OkHttpClient client = new OkHttpCl         


        
11条回答
  •  半阙折子戏
    2020-11-27 10:59

    Like so:

    //New Request
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    final OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(logging)
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .build();
    

提交回复
热议问题