【发布时间】:2017-02-06 11:39:29
【问题描述】:
我正在尝试解决一个问题,我将进行几个异步调用,并根据原始请求执行一项任务。为了解决这个问题,我尝试为每个请求添加一个标签,然后在成功响应后,我可以获取标签并根据标签采取行动。在这里,我只使用 TAG 来识别原始请求。
问题
在调用入队方法之前,我将标记设置为原始请求。但是当我在成功的回调中得到响应时,我得到了我没有设置的不同标签。不知何故,请求对象本身作为标记对象出现在那里。不知道怎么弄???
请检查下面的代码-
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());
}
});
有人可以指出我在这里做错了什么。任何帮助,将不胜感激。
【问题讨论】: