我正在使用Retrofit 2-beta2和OkHttp 2.7.0.
为了OkHttpClient从Retrofit 获取对象,我正在使用Retrofit .client()方法并取消所有正在运行的请求,我称之为取消(Object tag)方法,但请求仍然继续运行,我得到了响应.
即使客户Dispatcher的getQueuedCallCount()和getRunningCallCount()返回0调用cancel()之后.
还有什么我需要做的才能工作吗?或者它可能是OkHttp中的错误?
作为一种解决方法,我打电话shutdownNow()给客户,ExecutorService 但我更喜欢更清洁的解决方案.
我正在尝试解决一个问题,我将进行几个异步调用,并根据原始请求,我正在执行一项任务.为了解决这个问题,我正在尝试为每个请求添加一个TAG,然后成功响应,我可以获取标记并根据标记采取行动.在这里,我只使用TAG来识别原始请求.
问题
在调用enqueue方法之前,我将标记设置为原始请求.但是当我在成功的回调中得到响应时,我得到的是我没有设置的不同标签.不知何故,请求对象本身就是那里的标记对象.我不确定,怎么样?
请检查以下代码 -
GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class);
final Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit");
// Set the string tag to the original request object.
call.request().newBuilder().tag("hello").build();
call.enqueue(new Callback<List<Contributor>>() {
@Override
public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
Log.d("tag", response.raw().request().tag().toString());
// I'm getting Request{method=GET, url=https://api.github.com/repos/square/retrofit/contributors, tag=null} as the value of the tag. WHY????
final TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(response.body().toString());
}
@Override
public void onFailure(Call<List<Contributor>> call, Throwable t) {
final TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Something went wrong: " + t.getMessage()); …Run Code Online (Sandbox Code Playgroud)