在改造 2 中同时重复请求

sre*_*ree 6 android retrofit okhttp retrofit2

我面临单个 API 重复请求的问题,我使用的是改造 2.当我尝试调用一个 API 时,它命中服务器的次数是 3 倍。相同的 API 被多次调用,但只有一小部分。这是代码:

 public Retrofit retrofit() {
    String UrlBasePath="";

    if (mRetrofit == null) {
        if (builder == null) {
            builder = new OkHttpClient.Builder();
        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();
                HttpUrl httpUrl = original.url().newBuilder()
                        .build();
                String credentials = BuildConfig.ApiUserName + ":" + BuildConfig.ApiPassword;

                if (BuildConfig.ApiUserName.equals("APIUSERNAME") || BuildConfig.ApiPassword.equals("APIPASSWORD")) {
                    AnalyticsManager.sendEvent("RETROERROR", "AUTHENTICATIONFAILED", "FAILED");
                }

                final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                // Request customization: add request headers
                Request request = original.newBuilder()
                        .addHeader("Authorization", basic)
                        .addHeader("User-Agent", "android")
                        .method(original.method(), original.body())
                        .url(httpUrl)
                        .build();
                return chain.proceed(request);

            }
        }).connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS);

        if(BuildConfig.BUILD_TYPE.equalsIgnoreCase("debug")) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(logging);
        }

        UrlBasePath = UrlParser.httpsUrlBasePath;

        OkHttpClient client = enableTls12OnPreLollipop(builder).build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(UrlBasePath)
                .addConverterFactory(new ToStringConverterFactory())
                .addConverterFactory(GsonConverterFactory.create(gsonMapper()))
                .client(client)
                .build();
    }
    }
    return mRetrofit;
}
Run Code Online (Sandbox Code Playgroud)

在Activity中调用了这个方法:Activity代码

private final BmApiInterface RetroApiCall = RetroConnect.getInstance().retrofit().create(BmApiInterface.class); //global variable

Call<SingleLoginParser> loginCall = RetroApiCall.getLoginAPI(MatriidDet+"~"+Constants.APPVERSIONCODE,System.currentTimeMillis(),
                        Constants.constructApiUrlMap(new UrlParser().UrlGenerator(Constants.COMMON_LOGIN, new String[]{}))
                );
                mCallList.add(loginCall);
                RetroConnect.getInstance().AddToEnqueue(loginCall, mListener, RequestType.COMMON_LOGIN);
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗!!

Maj*_*man 6

当服务器响应缓慢且超时时,retfofit2 可能会多次重试相同的请求。为了防止这种情况,您必须使用.retryOnConnectionFailure(false)OkHttpClient 的方法。

示例代码:

OkHttpClient okHttpClient= null;
        okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(new TLSSocketFactory(),trustManager)
                .connectTimeout(2, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
                //.sslSocketFactory(sslSocketFactory, trustManager)
                .followRedirects(false)
                .followSslRedirects(false)
                .retryOnConnectionFailure(false)
                .cache(null)//new Cache(sContext.getCacheDir(),10*1024*1024)
                .build();
Run Code Online (Sandbox Code Playgroud)

在以前的版本中有一些错误并在改造后修复:2.1.0

所以你应该使用retrofit2和okhttp3的更新版本:

    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    implementation 'com.squareup.okhttp3:okhttp:3.4.1' 
Run Code Online (Sandbox Code Playgroud)

问题讨论:

https://github.com/square/okhttp/pull/1259#issuecomment-68430264