gpt4 book ai didi

android - 正在发送多个改造调用,但有些从未收到响应或错误

转载 作者:行者123 更新时间:2023-12-02 14:10:08 25 4
gpt4 key购买 nike

一般说明
当我在短时间内发送多个请求时,其中一些请求永远不会得到响应或出现错误。

API
我有一个单例类,可以在其中保留改造服务。我使用这个类来执行对 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);
}

调用
我在 Activity 中设置了一个按钮,每次单击该按钮时,都会执行对 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
}
});
}
});

问题
每当我单击按钮(不要太快)时,在日志中我可以看到该请求是通过改造发出的。但是,当我开始更快地单击按钮时,我可以在日志中看到正在发送请求,但并非所有请求都收到响应。没有错误,没有超时,什么都没有。在 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 负责按指定顺序从多个端点刷新用户数据时,我注意到它开始陷入随机点,有时在第二次调用,有时在第十次调用,有时在两者之间的某个地方。奇怪的是,在其中一个调用被卡住后,我仍然可以从应用程序中的其他位置执行其他调用。

最佳答案

 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
}
});

您正在授权 header 中传递字符串而不是实际值。将您的授权 token 代码更改为值而不是 key 。

关于android - 正在发送多个改造调用,但有些从未收到响应或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46296956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com