java.net.SocketTimeoutException: timeout

前端 未结 6 969
余生分开走
余生分开走 2021-02-03 18:27

With OkHttp library, application is facing following SocketTimeoutException issue. If request size is less, then it is working fine(Less than 1MB). I a

6条回答
  •  耶瑟儿~
    2021-02-03 18:53

    You need to understand that only adding this won't solve your problem:

    OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
    

    If you are using Kotlin + Retrofit + Coroutines then just use try and catch for network operations like,

    viewModelScope.launch(Dispatchers.IO) {
            try {
                val userListResponseModel = apiEndPointsInterface.usersList()
                returnusersList(userListResponseModel)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    

    Where, Exception is type of kotlin and not of java.lang

    This will handle every exception like,

    1. HttpException
    2. SocketTimeoutException
    3. FATAL EXCEPTION: DefaultDispatcher etc

    Here is my usersList() function

    @GET(AppConstants.APIEndPoints.HOME_CONTENT)
    suspend fun usersList(): UserListResponseModel
    

提交回复
热议问题