栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

OkHttp3从不慢互联网上的超时

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OkHttp3从不慢互联网上的超时

正如Trevor Halvorson指出的那样,您可以callTimeout在客户端
构建期间设置,方法是:

OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .callTimeout(20, TimeUnit.SECONDS) .writeTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .retryonConnectionFailure(false) .build();

我已经在使用以下版本

3.14.0
的虚拟项目中进行了个人测试okhttp3:

implementation 'com.squareup.okhttp3:okhttp:3.14.0'

And setting a timout of 5 seconds and my emulator connection to GPRS and
Poor connectivity I get

java.net.SocketExcpetion: Socket closed: timeout

This is my full dummy activity :

package com.example.shadowsheep.myapplication;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import java.io.IOException;import java.io.InterruptedIOException;import java.util.concurrent.TimeUnit;import androidx.appcompat.app.AppCompatActivity;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final TextView helloTextView = findViewById(R.id.helloTextView);        OkHttpClient client = new OkHttpClient.Builder()     .connectTimeout(5, TimeUnit.SECONDS)     .callTimeout(5, TimeUnit.SECONDS)     .writeTimeout(5, TimeUnit.SECONDS)     .readTimeout(5, TimeUnit.SECONDS)     .retryonConnectionFailure(false)     .build();        Request request = new Request.Builder()     .url("https://www.versionestabile.it/blog")     .build();        client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {     e.printStackTrace();     Log.d("OKHTTP3", e.getMessage());     // You get this failure     runonUiThread(() -> helloTextView.setText("TIMEOUT - FAILURE -> " + e.getMessage())); } @Override public void onResponse(Call call, Response response) throws IOException {     try {         final String _body = response.body().string();         Log.d("OKHTTP3", _body);         runonUiThread(() -> {  helloTextView.setText(_body);         });     } catch (InterruptedIOException e) {         runonUiThread(() -> {  // Or this exception depending when timeout is reached  helloTextView.setText("TIMEOUT EXCEPTION->"+ e.getCause() + ": " + e.getMessage());         });     } }        });    }}

I’ll give you also my app

build.gradle
file.

apply plugin: 'com.android.application'android {    compileSdkVersion 28    defaultConfig {        applicationId "com.example.shadowsheep.myapplication"        minSdkVersion 24        targetSdkVersion 28        versionCode 1        versionName "1.0"        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"    }    buildTypes {        release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'        }    }    compileOptions {        sourceCompatibility 1.8        targetCompatibility 1.8    }}dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'    implementation 'com.squareup.okhttp3:okhttp:3.14.0'}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号