如何在Robospice结果监听器中传递参数?

时间:2016-01-30 09:20:57

标签: android robospice

我有一个请求类型和几个请求参数。例如,我需要下载一些互联网页面,我有param = "pageNumber"。我打电话给:

mRequest = new SpiceRaspRequest(pageNumber);
mSpiceManager.execute(mRequest, new RequestResultListener());

但在onRequestSuccess / onRequestFailure我只有"结果"。如果我打电话给几个请求,我就不会知道它们是什么请求,因为结果可以异步发送。

是否可以知道 - 请求的结果是什么?

3 个答案:

答案 0 :(得分:0)

如果您想知道要获得结果的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);
    }
 } 
}

现在让我们说你的活动会听取结果,你会通过一个名为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
}

}

答案 1 :(得分:0)

如果您在页面下载失败时遇到问题,请执行以下操作:

当请求失败时,它会触发一个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);
    }
}

最后,在您的活动/片段中,您将获得无法下载的pageNumber:

onRequestFailed(SpiceException e) {

      if (e.getCause() instanceOf PageFailedException ) {
      PageFailedException pageFailedException = (PageFailedException) e.getCause();
      int pageFailedNumber = pageFailedException.getPageNumber();
      }
    }

答案 2 :(得分:0)

嗯,我发现更简单明了的方式。只需为每个请求创建单独的侦听器:

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) {
    }
}

多数民众赞成!无需覆盖大量代码。