多个Retrofit请求被发送,但有一些永远不会收到响应或错误。

4

概述
当我在短时间内发送多个请求时,其中一些请求没有收到响应或出现错误。

API
我有一个单例类,其中包含Retrofit服务。我使用这个类执行所有对API的调用,每个调用都返回一些类型的Observable数据。

public class CoreApi {

    public static final String BASE_URL = "https://www.example.com/";

    private static CoreApi instance;

    private CoreApiService service;

    public static CoreApi get() {
        if (instance == null)
            instance = new CoreApi();
        retrun instance;
    }

    private CoreApi() {

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        if (BuildConfig.DEBUG)
            httpClient.addInterceptor(httpLoggingInterceptor);

        httpClient.connectTimeout(60, TimeUnit.SECONDS);
        httpClient.readTimeout(60, TimeUnit.SECONDS);

        Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
            .client(httpClient.build());

        service = (builder.build()).create(CoreApiService.class);
    }

    public Observable<SomeData> getSomedata(String authorization) {
            return service.getSomeData(authorization);
    }

}

服务

interface CoreApiService {
    @GET("someDataEndpoint/")
    Observable<SomeData> getSomeData(@Header("Authorization") String authorizationToken);
}

CALL
我在活动中设置了一个按钮,每次点击它都会调用api:

Button button = (Button) findViewById(R.id.test_button);
button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CoreApi.get().getSomeData("JWT thisisexampletokenreplacedforthesakeofthisquestion")
                        .subscribeOn(Schedulers.io())
                        .observeOn(Schedulers.io())
                        .subscribe(new Action1<SomeData>() {
                            @Override
                            public void call(SomeData someData) {
                                 // operations are performed on data, irrelevant for the issue as even if I comment them the issue still occurs
                            }
                    });
                }
            });

问题
每当我点击按钮(不要太快),在日志中可以看到请求正在由Retrofit发出。 但是当我开始更快地点击按钮时,我可以在日志中看到请求正在发送,但并非所有请求都收到响应。没有错误,没有超时,没有任何提示。在Logcat中,我只能看到请求已经被发出(如下所示)。

09-19 11:26:05.421 18763-18821/com.myapp.app D/OkHttp: --> GET https://www.example.com/someDataEndpoint/ http/1.1
09-19 11:26:05.421 18763-18821/com.myapp.app D/OkHttp: Authorization: JWT thisisexampletokenreplacedforthesakeofthisquestion
09-19 11:26:05.422 18763-18821/com.myapp.app D/OkHttp: --> END GET

摘要
以上示例被简化了,但是只有在短时间内有大量调用时(不一定是到同一个端点),才会出现这个问题。 起初,当我负责从多个端点按指定顺序刷新用户数据的HandlerThread开始在随机点卡住时,我注意到了这一点,有时在第二次,有时在第十次调用,有时在中间某个位置。奇怪的是,当其中一个调用卡住后,我仍然可以从应用程序的其他位置执行其他调用。


什么是AhoyCoreApi()? - Sharath kumar
@匿名者,抱歉我的错,我更改了类的命名以使其更易读,我忘记了构造函数。请查看修改后的问题。 - PotatoMan
哦,我想知道为什么你没有遇到任何错误。 - Sharath kumar
@现实生活中的匿名用户,“授权令牌”是访问指定端点所需的实际授权令牌值。在这里,它只是一个例子,所以我用“授权令牌”代替了它。 - PotatoMan
但是我看到你的日志显示了键而不是值。 - Sharath kumar
1个回答

0
 CoreApi.get().getSomeData(//Your value here instead of the key)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.io())
                    .subscribe(new Action1<SomeData>() {
                        @Override
                        public void call(SomeData someData) {
                             // operations are performed on data, irrelevant for the issue as even if I comment them the issue still occurs
                        }
                });

在授权头中,您正在传递字符串而不是实际值。请将您的授权令牌代码更改为值而不是键。


2
问题不在于授权头,因为它正常工作。 问题是关于一些请求出现在日志中被发送,但从未收到任何响应或错误。 - PotatoMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接