【问题标题】:Handling error in RoboSpice处理 RoboSpice 中的错误
【发布时间】:2015-04-29 07:35:53
【问题描述】:

我是 Robospice 的新手,在处理错误请求时遇到问题。有没有办法可以从 onRequestFailure 方法中的错误请求访问响应正文?如果没有,你们是怎么做到的?

private class RequestListener implements RequestListener<Object> {
    @Override
    public void onRequestFailure(SpiceException e) {

    }

    @Override
    public void onRequestSuccess(Object response) {

    }
}

有人建议我应该在 Spice Request 中进行错误检查。有什么建议吗?

@Override
public SubscriptionsContainer loadDataFromNetwork() {
    ResponseEntity<SubscriptionsContainer> response = null;
    try {  
        response = getRestTemplate().exchange(/*your request data*/);
    } catch (HttpClientErrorException e) {
       String responsebody =  e.getResponseBodyAsString();
    }
}

【问题讨论】:

    标签: android error-handling robospice


    【解决方案1】:

    如果您从loadDataFromNetwork() 抛出任何异常,它将被包装为SpiceException 并作为参数传递给onRequestFailure()

    因此,您应该检查您的ResponseEntity&lt;T&gt; 以了解您期望的条件,并在未满足的情况下抛出异常。 另一方面,如果您的 RestTemplate.exchange 调用引发异常,您也可以处理它(如您的示例中所示)或让它被抛出(因此将在主线程上使用 onRequestFailure() 报告)。

    【讨论】:

      猜你喜欢
      • 2014-09-21
      • 1970-01-01
      • 2015-08-23
      • 2016-08-18
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      相关资源
      最近更新 更多