如何在 Robospice 结果监听器中传递参数?
How to pass parameters in Robospice result listener?
我有一个请求类型和几个请求参数。例如,我需要下载一些互联网页面并且我有 param = "pageNumber"
。我打电话给:
mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, new RequestResultListener());
但是在 onRequestSuccess
/onRequestFailure
我只有 "result"。如果我调用多个请求,我将不知道它们是什么请求,因为结果可以异步触发。
是否可以知道 - 什么请求的结果是什么?
如果您想知道获得结果的 pageNumber,您需要在下载后在结果对象中设置 pageNumber。
为此,您将在 SpiceRaspRequest 和结果对象中添加几行(我们称结果对象为 RaspPage)
public class RaspPage {
....
private int mPageNumber;
public getPageNumber() {
return mPageNumber;
}
public setPageNumber(int pageNumber) {
mPageNumber = pageNumber;
}
public class SpiceRaspRequest extends SpringAndroidSpiceRequest<RaspPage>{
int mPageNumber;
public SpiceRaspRequest(int pageNumber) {
mPageNumber = pageNumber;
RestTemplate restTemplate= getRestTemplate();
// add these lines so that the Robospice does not look for the field //pageNumber that you have added to the RestPage class.
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converter.getObjectMapper().disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
restTemplate.getMessageConverters().add(converter);
}
@Overrides
public RaspPage loadDataFromNetwork throws Exception {
...
try {
// before returning the result you need to set the pageNumber
RaspPage result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, RaspPage.class);
result.setPageNumber(mPageNumber)
return result;
}catch (HttpClientErrorException e) {
message = e.getResponseBodyAsString();
throw new SpiceException(message);
}catch (HttpServerErrorException e) {
message = e.getResponseBodyAsString();
throw new SpiceException(message);
}
}
}
现在假设您的 activity 监听结果,您将通过名为 onRequestSuccess
的回调获得结果
MainActivity extends Activity implements RequestListener<RaspPage> {
private SpiceRaspRequest;
@Overrides
public void onCreate(Bundle onSavedInstanceState) {
super.onCreate();
...
mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, this);
}
@Overrides
public void onRequestSuccess(RaspPage result) {
int pageNumber = result.getPageNumber();
// This is the page number you are looking for
}
@Overrides
public void onRequestFailure(SpiceException spiceException) {
// TODO : do something if you failed to download the page
}
}
如果您在页面下载失败时遇到问题,请执行以下操作:
当请求失败时,它会触发一个 RestClientException,该异常将在回调方法 onRequestFailed(SpiceException e) 的参数中
为了知道哪个 pageNumber 下载失败,您可以扩展一个 SpiceException
public class PageFailedException extends RestClientException {
public int mPageNumber;
public PageFailedException(String detailMessage) {
super(detailMessage);
}
public int getPageNumber() {
return mPageNumber;
}
public void setPageNumber(int mPageNumber) {
this.mPageNumber = mPageNumber;
}
}
然后在您的 loadDataFromNetwork() 方法中,您应该捕获 SpiceException 并设置相应的 pageNumber:
try {
ResponseEntity<RaspPage> result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, RaspPage.class);
RaspPage raspPage = result.getBody();
raspPage.setPageNumber(mPageNumber);
return raspPage;
}catch (PageFailedException e) {
e.setPageNumber(mPageNumber);
throw new SpiceException(e);
}
}
最后,在您的 activity/fragment 中,您将获得下载失败的页码:
onRequestFailed(SpiceException e) {
if (e.getCause() instanceOf PageFailedException ) {
PageFailedException pageFailedException = (PageFailedException) e.getCause();
int pageFailedNumber = pageFailedException.getPageNumber();
}
}
好吧,我找到了更简单明了的方法。只需为每个请求创建单独的侦听器:
mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, new RequestResultListener(pageNumber));
然后在听众中:
private class RequestResultListener implements RequestListener<RaspRequest> {
private int pageNumber;
public RequestResultListener(int pageNumber) {
this.pageNumber = pageNumber;
}
@Override
public void onRequestFailure(SpiceException e) {
}
@Override
public void onRequestSuccess(RaspRequest raspRequest) {
}
}
就是这样!无需重写大量代码。
我有一个请求类型和几个请求参数。例如,我需要下载一些互联网页面并且我有 param = "pageNumber"
。我打电话给:
mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, new RequestResultListener());
但是在 onRequestSuccess
/onRequestFailure
我只有 "result"。如果我调用多个请求,我将不知道它们是什么请求,因为结果可以异步触发。
是否可以知道 - 什么请求的结果是什么?
如果您想知道获得结果的 pageNumber,您需要在下载后在结果对象中设置 pageNumber。
为此,您将在 SpiceRaspRequest 和结果对象中添加几行(我们称结果对象为 RaspPage)
public class RaspPage {
....
private int mPageNumber;
public getPageNumber() {
return mPageNumber;
}
public setPageNumber(int pageNumber) {
mPageNumber = pageNumber;
}
public class SpiceRaspRequest extends SpringAndroidSpiceRequest<RaspPage>{
int mPageNumber;
public SpiceRaspRequest(int pageNumber) {
mPageNumber = pageNumber;
RestTemplate restTemplate= getRestTemplate();
// add these lines so that the Robospice does not look for the field //pageNumber that you have added to the RestPage class.
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converter.getObjectMapper().disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
restTemplate.getMessageConverters().add(converter);
}
@Overrides
public RaspPage loadDataFromNetwork throws Exception {
...
try {
// before returning the result you need to set the pageNumber
RaspPage result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, RaspPage.class);
result.setPageNumber(mPageNumber)
return result;
}catch (HttpClientErrorException e) {
message = e.getResponseBodyAsString();
throw new SpiceException(message);
}catch (HttpServerErrorException e) {
message = e.getResponseBodyAsString();
throw new SpiceException(message);
}
}
}
现在假设您的 activity 监听结果,您将通过名为 onRequestSuccess
的回调获得结果MainActivity extends Activity implements RequestListener<RaspPage> {
private SpiceRaspRequest;
@Overrides
public void onCreate(Bundle onSavedInstanceState) {
super.onCreate();
...
mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, this);
}
@Overrides
public void onRequestSuccess(RaspPage result) {
int pageNumber = result.getPageNumber();
// This is the page number you are looking for
}
@Overrides
public void onRequestFailure(SpiceException spiceException) {
// TODO : do something if you failed to download the page
}
}
如果您在页面下载失败时遇到问题,请执行以下操作:
当请求失败时,它会触发一个 RestClientException,该异常将在回调方法 onRequestFailed(SpiceException e) 的参数中
为了知道哪个 pageNumber 下载失败,您可以扩展一个 SpiceException
public class PageFailedException extends RestClientException {
public int mPageNumber;
public PageFailedException(String detailMessage) {
super(detailMessage);
}
public int getPageNumber() {
return mPageNumber;
}
public void setPageNumber(int mPageNumber) {
this.mPageNumber = mPageNumber;
}
}
然后在您的 loadDataFromNetwork() 方法中,您应该捕获 SpiceException 并设置相应的 pageNumber:
try {
ResponseEntity<RaspPage> result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, RaspPage.class);
RaspPage raspPage = result.getBody();
raspPage.setPageNumber(mPageNumber);
return raspPage;
}catch (PageFailedException e) {
e.setPageNumber(mPageNumber);
throw new SpiceException(e);
}
}
最后,在您的 activity/fragment 中,您将获得下载失败的页码:
onRequestFailed(SpiceException e) {
if (e.getCause() instanceOf PageFailedException ) {
PageFailedException pageFailedException = (PageFailedException) e.getCause();
int pageFailedNumber = pageFailedException.getPageNumber();
}
}
好吧,我找到了更简单明了的方法。只需为每个请求创建单独的侦听器:
mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, new RequestResultListener(pageNumber));
然后在听众中:
private class RequestResultListener implements RequestListener<RaspRequest> {
private int pageNumber;
public RequestResultListener(int pageNumber) {
this.pageNumber = pageNumber;
}
@Override
public void onRequestFailure(SpiceException e) {
}
@Override
public void onRequestSuccess(RaspRequest raspRequest) {
}
}
就是这样!无需重写大量代码。