2017-07-07 38 views
6

リクエストを作成し、そのリクエストが4xxエラーを返した場合Retrofit/Okhttpはリクエストを再試行し続けます。 retryOnConnectionFailureをfalseに設定し、15秒のタイムアウトを設定しましたが、すべて無視されるようです。私は何か見落としてますか?Okhttpは常に失敗した接続を再試行します

private static OkHttpClient getClient() { 
     return new OkHttpClient.Builder() 
       .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE)) 
       .readTimeout(15, TimeUnit.SECONDS) 
       .connectTimeout(15, TimeUnit.SECONDS) 
       .retryOnConnectionFailure(false) 
       .addInterceptor(chain -> { 
        Request request = chain.request() 
          .newBuilder() 
          .build(); 

        return chain.proceed(request); 
       }).build(); 
    } 

    public static Retrofit getRetrofitClient(Gson gson){ 
     Retrofit.Builder builder = new Retrofit.Builder() 
       .baseUrl(baseUrl) 
       .client(OkHttpLogger.getClient()); 
     if(gson != null){ 
      builder.addConverterFactory(GsonConverterFactory.create(gson)); 
     }else{ 
      builder.addConverterFactory(GsonConverterFactory.create()); 
     } 

     return builder.build(); 
    } 

私は私が見てい

ことの一つは、私は5秒のタイムアウトを設定した場合、それは5秒間の再試行を続けて完璧に動作していることである編集改造2.3.0とokhttpバージョン3.8.1

を使用していますそれから私に失敗を与えるが、もし私が10秒にそれをバンプすれば、それはちょうど行くことを続け、最終的に2分ほどで止まる。

+0

依頼を続行するのはあなたの傍受者ですか?サイレントリクエストの再試行を無効にするための既知のスイッチとして 'retryOnConnectionFailure(false)'を実装するのは正しいです。 –

+0

@ M.Palsichはインターセプタを削除しても効果がありません。私が見ていることの1つは、5秒のタイムアウトを設定すると完全に動作して5秒間リトライしてから失敗しますが、10秒間バンプすれば2分ほどで終了します。 – tyczj

+0

TCP SYN再試行のための独自の設定を持つ環境内に存在するエミュレータでテストしていますか?たとえば、Linuxは独自のTCP SYN再試行制限を設定できます。 –

答えて

1

問題はretryOnConnectionFailureが適用されないということです408に応答しますので、自動的にそれらを再試行します

2

このコードは、私がアプリケーションクラスにレトロフィットを使用して、アプリケーション名のタグ

public final class Application extends MultiDexApplication { 

public static Retrofit retrofit; 
public static String base_URL = "http://mds.devsiteurl.com/"; 

public static final String TAG = Application.class.getSimpleName(); 

private static Application mInstance; 

public static synchronized Application getInstance() { 
    return mInstance; 
} 
@Override 
public void onCreate() { 
    super.onCreate(); 

    mInstance = this; 
    MultiDex.install(this); 

    Gson gson = new GsonBuilder() 
      .setLenient() 
      .create(); 

    final OkHttpClient okHttpClient = new OkHttpClient.Builder() 
      .readTimeout(15, TimeUnit.SECONDS) 
      .connectTimeout(15, TimeUnit.SECONDS) 
      .build(); 

    retrofit = new Retrofit.Builder() 
      .baseUrl(base_URL) 
      .client(okHttpClient) 
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
      .addConverterFactory(GsonConverterFactory.create(gson)) 
      .build(); 
} 
@Override 
protected void attachBaseContext(Context base) { 
    super.attachBaseContext(base); 
    MultiDex.install(this); 
} 
} 

とマニフェストファイル内

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mds.foundation.mdsfoundation"> 

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:name=".Application" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_apis_keys" /> 

    <activity android:name=".activity.MainActivity" /> 
    <activity 
     android:name=".activity.CenterOfExcellenceActivity" 
     android:windowSoftInputMode="adjustPan" /> 
    <activity android:name=".activity.SplashScreenActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 

</manifest> 

そして最後としてマニフェストファイルにそのクラスを使用して、あなたを助けるかもしれgradle.build

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 25 
buildToolsVersion "25.0.3" 
defaultConfig { 
    applicationId "com.mds.foundation.mdsfoundation" 
    minSdkVersion 19 
    targetSdkVersion 25 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    multiDexEnabled true //this line is important 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
packagingOptions { 

    exclude 'META-INF/DEPENDENCIES.txt' 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/DEPENDENCIES' 
    exclude 'META-INF/notice.txt' 
    exclude 'META-INF/license.txt' 
    exclude 'META-INF/dependencies.txt' 
    exclude 'META-INF/LGPL2.1' 
} 
useLibrary 'org.apache.http.legacy' 
} 

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
compile('org.apache.httpcomponents:httpmime:4.3.6') { 
    exclude module: 'httpclient' 
} 
compile 'org.apache.httpcomponents:httpclient-android:4.3.5' 
compile 'com.android.support:appcompat-v7:25.3.1' 
compile 'com.android.support.constraint:constraint-layout:1.0.1' 
compile 'com.android.support:design:25.3.1' 
compile 'com.android.support:support-v4:25.3.1' 

//refrofit 
compile 'com.squareup.retrofit2:retrofit:2.1.0' 
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' 
compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
compile 'com.android.support:multidex:1.0.1' 
compile 'com.google.code.gson:gson:2.6.2' 
compile 'com.squareup.okhttp3:okhttp:3.2.0' 
compile 'com.squareup.picasso:picasso:2.5.2' 
testCompile 'junit:junit:4.12' 
} 
+0

これは、基本的に私が持っているそれは助けになりません – tyczj

+0

アプリケーションクラスで 'MultiDexApplication'を拡張しましたか? –

+0

はい、それはokhttpとretrofitの使用とは無関係です – tyczj

関連する問題