一般说明
当我在短时间内发送多个请求时,其中一些请求永远不会得到响应或错误。
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);
}
拨打
我在活动中设置了一个按钮,每次单击它时,它都会调用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负责从指定序列中的几个端点刷新用户数据时开始陷入随机点,有时是第2次,有时是第10次调用,有时是介于两者之间。奇怪的是,在其中一个电话卡住之后,我仍然可以在应用程序的其他地方执行其他调用。
答案 0 :(得分:0)
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard userActivity.activityType == Employee.domainIdentifier,
let objectId = userActivity.userInfo?["id"] as? String else {
return false
}
if let nav = window?.rootViewController as? UINavigationController,
let listVC = nav.viewControllers.first as? EmployeeListViewController,
let employee = EmployeeService().employeeWithObjectId(objectId) {
nav.popToRootViewController(animated: false)
let employeeViewController = listVC
.storyboard?
.instantiateViewController(withIdentifier: "EmployeeView") as!
EmployeeViewController
employeeViewController.employee = employee
nav.pushViewController(employeeViewController, animated: false)
return true
}
return false
}
您在授权标头中传递字符串而不是实际值。将授权令牌代码更改为值而不是键。