2017-07-07 26 views
6

यदि मैं कोई अनुरोध करता हूं और वह अनुरोध 4xx त्रुटि देता है तो रेट्रोफिट/Okhttp अनुरोध को पुनः प्रयास करता रहता है। मैंने retryOnConnectionFailure को झूठा करने के लिए सेट किया है और 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(); 
    } 

मैं पुराना वापस 2.3.0 और okhttp संस्करण का उपयोग कर रहा 3.8.1

संपादित

एक बात मैं देख पा रहे हैं कि अगर मैं 5 सेकंड का समय समाप्त सेट यह पूरी तरह से काम करता है 5 सेकंड के लिए पुन: प्रयास रहता है तो मुझे एक विफलता देता है लेकिन अगर मैं इसे 10 सेकंड तक टक्कर देता हूं तो यह अभी भी चलता रहता है और अंततः लगभग 2 मिनट तक बंद हो जाता है।

+0

क्या आपका इंटरसेप्टर अनुरोध जारी रखने की अनुमति देने के लिए ज़िम्मेदार है? आप चुप अनुरोध अनुरोधों को अक्षम करने के लिए ज्ञात स्विच के रूप में 'retryOnConnectionFailure (false)' को लागू करने में सही हैं। –

+0

@ एम। पालसीच इंटरसेप्टर को हटाने का कोई प्रभाव नहीं पड़ता है। एक चीज जो मैं देखता हूं वह यह है कि यदि मैं 5 सेकंड का टाइमआउट सेट करता हूं तो यह पूरी तरह से 5 सेकंड के लिए पुनः प्रयास करता रहता है तो मुझे विफलता देता है लेकिन अगर मैं इसे 10 सेकंड तक टक्कर देता हूं तो यह अभी भी चलता रहता है और आखिरकार 2 मिनट तक बंद हो जाता है। – tyczj

+0

क्या आप एक ऐसे एमुलेटर में परीक्षण कर रहे हैं जो पर्यावरण के भीतर मौजूद है जिसमें टीसीपी एसवाईएन रीट्रीज़ की अपनी सेटिंग है? उदाहरण के लिए, लिनक्स अपनी टीसीपी एसवाईएन पुनः प्रयास सीमा निर्धारित कर सकता है। –

उत्तर

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

है क्या आपने अपनी एप्लिकेशन क्लास में 'मल्टीडेक्स एप्लिकेशन' बढ़ाया है? –

+0

हां लेकिन यह okhttp का उपयोग करने के लिए असंबंधित है और – tyczj

संबंधित मुद्दे