由于似乎没有人有实现这一目标的好方法,因此我添加了我的解决方案(不是最好的解决方案),以防其他人有类似的需求。
我继承了 SpringAndroidSpiceRequest 并在 loadDataFromNetwork() 中获得了 RestTemplate 并简单地发出 x 多个请求并将它们合并我自己,然后返回组合数据作为 spiceRequest 的结果。
当我找到更好的方法时,我将替换此代码,但现在它可以完成工作。
public class LargeSpiceRequest extends SpringAndroidSpiceRequest<LargeResponse>{
private String mUrl;
public LargeSpiceRequest(String url) {
super(LargeResponse.class);
mUrl = url;
}
@Override
public LargeResponse loadDataFromNetwork() throws Exception {
RestTemplate restTemplate = getRestTemplate();
ResponseEntity<LargeResponse> largeResponseResponseEntity = restTemplate.exchange(URI.create(mUrl), HttpMethod.GET, null, LargeResponse.class);
LargeResponse largeResponse = largeResponseResponseEntity.getBody();
ArrayList<SubRequest> subRequests = largeResponse.getSubRequests();
for(SubRequest subRequest : subRequests){
try{
String url = subRequest.getUrl();
ResponseEntity<SubRequestResponse> subRequestResponseEntity = restTemplate.exchange(URI.create(url), HttpMethod.GET, null, SubRequestResponse.class);
SubRequestResponse subRequestResponse = subRequestResponseEntity.getBody();
// merge somethingResponse with largeResponse
largeResponse.mergeIn(subRequestResponse);
} catch (RestClientException e) {
// Handle this if you want
}
}
// the merged largeResponse will be stored in the cache
return largeResponse;
}
}