改造添加标签到原始请求对象 [英] Retrofit Adding tag to the original request object
问题描述
我正在尝试解决一个问题,在该问题中,我将进行几个异步调用,并根据原始请求执行任务.为了解决此问题,我尝试向每个请求添加一个TAG,然后在成功响应后,我可以获取该标签并根据该标签采取措施.在这里,我仅使用TAG来标识原始请求.
I'm trying to solve a problem where I'll be making a couple of asynchronous calls and based on the original request, I'm performing a task. To solve this issue, I'm trying to add a TAG to each request and then on successful response, I can get the tag and take action based on the tag. Here, I'm using TAG only to identify the original request.
问题
在调用enqueue方法之前,我将标签设置为原始请求.但是,当我在成功的回调中获得响应时,我得到的是我未设置的其他标签.请求对象本身以某种方式作为标签对象出现.我不确定如何?
Before calling the enqueue method, I'm setting the tag to the original request. But when I get the response in the successful callback, I'm getting different tag that I didn't set. Somehow the request object itself is coming as the tag object there. I'm not sure, how???
请检查下面的代码-
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());
}
});
有人可以指出我在这里到底在做什么错.任何帮助,将不胜感激.
Can somebody point out that what exactly I'm doing wrong here. Any help would be appreciated.
推荐答案
此解决方案显然是一种破解,但是可以.
This solution is clearly a hack, but it works.
假设您像这样创建自己的翻新服务:
Let's say you create your Retrofit service like this :
public <S> S createService(Class<S> serviceClass) {
// Could be a simple "new"
Retrofit.Builder retrofitBuilder = getRetrofitBuilder(baseUrl);
// Could be a simple "new"
OkHttpClient.Builder httpClientBuilder = getOkHttpClientBuilder();
// Build your OkHttp client
OkHttpClient httpClient = httpClientBuilder.build();
Retrofit retrofit = retrofitBuilder.client(httpClient).build();
return retrofit.create(serviceClass);
}
您将需要在Retrofit实例中添加一个新的CallFactory
,因此它每次都会添加一个标签.由于该标记是只读的,因此我们将使用仅包含一个元素的Object数组,稍后您将可以对其进行更改.
You will need to add a new CallFactory
to your Retrofit instance, so it adds a tag every-time. Since the tag will be read-only, we will use an array of Object containing only one element, which you will be able to change later on.
Retrofit retrofit = retrofitBuilder.client(httpClient).callFactory(new Call.Factory() {
@Override
public Call newCall(Request request) {
request = request.newBuilder().tag(new Object[]{null}).build();
Call call = httpClient.newCall(request);
// We set the element to the call, to (at least) keep some consistency
// If you want to only have Strings, create a String array and put the default value to null;
((Object[])request.tag())[0] = call;
return call;
}
}).build();
现在,在创建呼叫之后,您将能够更改标签的内容:
Now, after creating your call, you will be able to change the contents of your tag:
((Object[])call.request().tag())[0] = "hello";
这篇关于改造添加标签到原始请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!