How to set timeout with Okhttp3 and GlideV4?
By : Krishna Garikipati
Date : March 29 2020, 07:55 AM
wish helps you You can change the timeout by creating a class, which extends AppGlideModule and has annotation @GlideModule. Then you override the method registerComponents and inside it, you can create a new OkHttpClient, which you can use to control the Glide request timeout. First, you should add Glide and OkHttp3 Glide Integration library gradle dependencies in the build.gradle file: code :
compile 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
compile("com.github.bumptech.glide:okhttp3-integration:4.2.0") {
exclude group: 'glide-parent'
}
@GlideModule
public class MyGlideAppModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
}
}
|
How to set connection timeout in okhttp3?
By : Seba Seb
Date : March 29 2020, 07:55 AM
To fix this issue How can I set time out in this code? code :
client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).build()
|
OkHttp3 Never Timeout on slow internet
By : user2522382
Date : March 29 2020, 07:55 AM
this will help As pointed out by Trevor Halvorson, you could set callTimeout during client builder, in this way: code :
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.callTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
implementation 'com.squareup.okhttp3:okhttp:3.14.0'
java.net.SocketExcpetion: Socket closed: timeout
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());
});
}
}
});
}
}
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'
}
|
java.lang.AbstractMethodError: abstract method "okio.Timeout okhttp3.Call.timeout()"
By : Todd S
Date : March 29 2020, 07:55 AM
it should still fix some issue After some research on investigating this issue here is what I found out: Apparently, this crash is caused due to a timeout error when the React Native package uses okhttp3 by the time a React Native application instance is created.
|
Okhttp3 set timeout is useless
By : Blessing
Date : March 29 2020, 07:55 AM
|