[英]How do I know what OkHttp call I am returning from
I have an activity that implements okhttp3.Callback and overrides the onFailure and onResume methods. 我有一个实现okhttp3.Callback的活动,并覆盖onFailure和onResume方法。 There are multiple different requests called in this activity, something similar to this: 此活动中调用了多个不同的请求,类似于:
Request request1 = RequestBuilder.login(username,password);
okHttpClient.newCall(request1).enqueue(this);
Request request2 = RequestBuilder.getData(token);
okHttpClient.newCall(request2).enqueue(this);
How can I know if I am returning from request1 or request2 in the onResponse method, since the response needs to be handled differently for each request? 如何在onResponse方法中从request1或request2返回,因为响应需要针对每个请求进行不同的处理?
Option 1: in onResponse(Response response)
use response.request()
to get the RequestObject
and compare that with yours. 选项1:在onResponse(Response response)
使用response.request()
来获取RequestObject
并与之进行比较。
Option 2: instead of this
, use a anonymous class, like so: 选项2:而不是this
,使用匿名类,如下所示:
okHttpClient.newCall(request1).enqueue(new Callback() {
@Override
public void onFailure(Request request, Throwable throwable) {
...
}
@Override
public void onResponse(Response response) throws IOException {
...
}
});
and again for the second request: 并再次为第二个请求:
okHttpClient.newCall(request2).enqueue(new Callback() {
@Override
public void onFailure(Request request, Throwable throwable) {
...
}
@Override
public void onResponse(Response response) throws IOException {
...
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.