使用 OkHttp 调用多个异步请求时,有时会干扰回调
while calling more than one async requests with OkHttp, sometimes callbacks are interfered
我在 CustomerService class 上有两个方法,当我在 HomeFragment 上调用它们时,五次之一,我看到在 getTransactions 回调中我得到了 getCard 服务响应
public class CustomerService {
OkHttpClient okHttpClient = new OkHttpClient();
.....
public void getTransactions(String memberId, Callback callback)
{
List<Pair> params = new ArrayList<>();
params.add(new Pair<>("member_id", memberId));
String url = BASE_URL + "customer/gettransaction/?" + getQuery(params);
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
public void getCard(String memberId, Callback callback)
{
List<Pair> params = new ArrayList<>();
params.add(new Pair<>("member_id", memberId));
String url = BASE_URL + "customer/getcard/?" + getQuery(params);
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
}
在片段 class 中,我这样称呼它们:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_activities, container, false);
realm = Realm.getDefaultInstance();
getCardData();
getTransactions();
....
return rootView;
}
private void getTransactions() {
final User currentUser = UserRepository.getUser(realm);
customerService.getTransactions(currentUser.getMember_id(), new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.d(TAG, "Transaction service error: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Type responseType = new TypeToken<WsResponse<List<WsTransaction>>>() {}.getType();
final List<WsTransaction> wsTransactionList = LoyaltyService.getWsResponseData(response, responseType);
if (wsTransactionList != null) {
....
}
}
});
}
private void getCardData() {
final User currentUser = UserRepository.getUser(realm);
customerService.getCard(currentUser.getMember_id(), new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.d(TAG, "Customer - GetCard service error: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Type responseType = new TypeToken<WsResponse<WsCard>>() {}.getType();
final WsCard wsCard = LoyaltyService.getWsResponseData(response, responseType);
if (wsCard != null) {
.....
}
}
});
}
如果我延迟调用getTransactions 半秒,那么它没有问题。或者当我调试时,我没有任何问题。有人可以帮助我了解我所缺少的吗?
带回调的入队是异步的,因此很可能您正在使用相同的回调但没有等待登录成功。如果是这种情况,并且您期望结果的特定顺序,那么第二次 getCard 请求很可能有时在登录之前完成。
需要更多上下文来确认是否可能是这种情况。
原来是服务器端的问题。
我在 CustomerService class 上有两个方法,当我在 HomeFragment 上调用它们时,五次之一,我看到在 getTransactions 回调中我得到了 getCard 服务响应
public class CustomerService {
OkHttpClient okHttpClient = new OkHttpClient();
.....
public void getTransactions(String memberId, Callback callback)
{
List<Pair> params = new ArrayList<>();
params.add(new Pair<>("member_id", memberId));
String url = BASE_URL + "customer/gettransaction/?" + getQuery(params);
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
public void getCard(String memberId, Callback callback)
{
List<Pair> params = new ArrayList<>();
params.add(new Pair<>("member_id", memberId));
String url = BASE_URL + "customer/getcard/?" + getQuery(params);
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
}
在片段 class 中,我这样称呼它们:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_activities, container, false);
realm = Realm.getDefaultInstance();
getCardData();
getTransactions();
....
return rootView;
}
private void getTransactions() {
final User currentUser = UserRepository.getUser(realm);
customerService.getTransactions(currentUser.getMember_id(), new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.d(TAG, "Transaction service error: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Type responseType = new TypeToken<WsResponse<List<WsTransaction>>>() {}.getType();
final List<WsTransaction> wsTransactionList = LoyaltyService.getWsResponseData(response, responseType);
if (wsTransactionList != null) {
....
}
}
});
}
private void getCardData() {
final User currentUser = UserRepository.getUser(realm);
customerService.getCard(currentUser.getMember_id(), new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.d(TAG, "Customer - GetCard service error: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Type responseType = new TypeToken<WsResponse<WsCard>>() {}.getType();
final WsCard wsCard = LoyaltyService.getWsResponseData(response, responseType);
if (wsCard != null) {
.....
}
}
});
}
如果我延迟调用getTransactions 半秒,那么它没有问题。或者当我调试时,我没有任何问题。有人可以帮助我了解我所缺少的吗?
带回调的入队是异步的,因此很可能您正在使用相同的回调但没有等待登录成功。如果是这种情况,并且您期望结果的特定顺序,那么第二次 getCard 请求很可能有时在登录之前完成。
需要更多上下文来确认是否可能是这种情况。
原来是服务器端的问题。