Приложение падает на java.net.SocketTimeoutException: время ожидания (Kotlin, Retrofit) - PullRequest
0 голосов
/ 19 ноября 2018

Приложение падает в этой строке, если ответ не приходит.

chain.proceed(requestBuilder.build())

Вот мой класс для RetrofitClient

class RetrofitClient {

    private val apiService: ApiServiceInterface

    init {
        val builder = Retrofit.Builder()
        builder.baseUrl(RequestParameters.BASE_URL)
        builder.addConverterFactory(GsonConverterFactory.create())
        builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//added for adapter
        builder.client(getClient())
        val retrofit = builder.build()
        apiService = retrofit.create(ApiServiceInterface::class.java)
    }

    companion object {
        private var clientInstance: RetrofitClient? = null
        lateinit var context: Context

        fun getInstance(): RetrofitClient {
            if (clientInstance == null) {
                clientInstance = RetrofitClient()
            }
            return clientInstance as RetrofitClient
        }
    }

    fun getApiInterface(): ApiServiceInterface {
        return apiService
    }

    private fun getClient(): OkHttpClient {

        val httpClient = OkHttpClient.Builder()
        httpClient.readTimeout(25, TimeUnit.SECONDS)
        httpClient.connectTimeout(25, TimeUnit.SECONDS)

        httpClient.addInterceptor { chain ->
            val original = chain.request()

            val requestBuilder = original.newBuilder()
                    .header(RequestParameters.X_API, RequestParameters.H_XAPI_KEY)
                    .method(original.method(), original.body())

            chain.proceed(requestBuilder.build())
        }

        // set your desired log level
        val logging = HttpLoggingInterceptor()
        logging.level = HttpLoggingInterceptor.Level.HEADERS
        logging.level = HttpLoggingInterceptor.Level.BODY
        httpClient.addInterceptor(logging)

        return httpClient.build()
    }
}

Вот ошибка в журнале:

1542609097.397 26572-26572/com.app.mylife W/System.err: java.net.SocketTimeoutException: timeout
1542609097.397 26572-26572/com.app.mylife W/System.err:     at okio.Okio$4.newTimeoutException(Okio.java:230)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err:     at com.app.mylife.retrofit.RetrofitClient$getClient$1.intercept(RetrofitClient.kt:64)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at okhttp3.RealCall.execute(RealCall.java:77)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at io.reactivex.Scheduler$1.run(Scheduler.java:134)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.lang.Thread.run(Thread.java:764)
1542609097.399 26572-26572/com.app.mylife W/System.err: Caused by: java.net.SocketException: Socket closed
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:203)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:139)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at okio.Okio$2.read(Okio.java:139)
1542609097.399 26572-26572/com.app.mylife W/System.err:     at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
1542609097.400 26572-26572/com.app.mylife W/System.err:     ... 39 more

Ответы [ 2 ]

0 голосов
/ 21 ноября 2018

Чтобы устранить эту проблему, необходимо обязательно проверить сетевое подключение на устройстве и убедиться, что у вас есть активное подключение к Интернету, прежде чем совершать сетевой вызов.Способы проверки подключения к Интернету перед выполнением сетевых запросов. Вы можете использовать диспетчер подключений как таковой

fun isOnline(): Boolean {
    val cm =
        getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val netInfo = cm.getActiveNetworkInfo()
    return netInfo != null && netInfo.isConnectedOrConnecting()
}

, который был выбран из этого ответа , который позволяет проверить доступность сети.Если вы хотите более точно отслеживать, активна ли ваша сеть или нет, вы можете попробовать пинговать Google DNS-серверы с ответом здесь Проверка сети на Android .

0 голосов
/ 19 ноября 2018

Попробуйте этот способ кода .. сделать часть создания объекта модернизации.

class ApiClient {

companion object {
    val BASE_URL = "https://simplifiedcoding.net/demos/"
    var retrofit: Retrofit? = null
    fun getClient(): Retrofit? {
        if (retrofit == null) {
            val interceptor = HttpLoggingInterceptor()
            interceptor.level = HttpLoggingInterceptor.Level.BODY
            val client = OkHttpClient.Builder().apply {
            readTimeout(20, TimeUnit.SECONDS)
            writeTimeout(20, TimeUnit.SECONDS)
            connectTimeout(20, TimeUnit.SECONDS)
            addInterceptor(interceptor)
            addInterceptor { chain ->
                var request = chain.request()
                request = request.newBuilder()
                        .build()
                val response = chain.proceed(request)
                response
            }
            }
            retrofit = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client.build())

                    .addConverterFactory(GsonConverterFactory.create())
                    .build()

        }

        return retrofit
    }
}

}

создать интерфейс для вызова API ..

interface ApiInterface {
@GET("marvel")
fun getData(): Call<List<Hero>>

}

В упражнении или фрагменте, называемом api, вот так, я надеюсь, вы создадите ответ pojo класс

var apiInterface: ApiInterface = ApiClient.getClient()!!.create(ApiInterface::class.java)
    var hero: Call<List<Hero>>
    hero = apiInterface.getData()
    hero.enqueue(object : Callback<List<Hero>> {
        override fun onFailure(call: Call<List<Hero>>?, t: Throwable?) {
            closeDialog(dialog)
            Toast.makeText(mContext, t?.message, Toast.LENGTH_SHORT).show()
            Log.d("Error:::",t?.message)
        }

        override fun onResponse(call: Call<List<Hero>>?, response: Response<List<Hero>>?) {
           mHeroDataList.clear()
            if (response != null && response.isSuccessful && response.body() != null) {
                closeDialog(dialog)
                mHeroDataList .addAll(response.body()!!)
                setAdapter(mHeroDataList)
            }
        }

    })
...