【问题标题】:Throw exception if response code isn't 20x如果响应代码不是 20x,则抛出异常
【发布时间】:2017-03-23 13:11:56
【问题描述】:

我正在使用 Koush Ion 库,我想知道如果响应代码不是 20 倍(例如,如果它是 400、401 等),是否可以选择抛出异常。

【问题讨论】:

    标签: android android-ion ion-koush


    【解决方案1】:

    您可以简单地检查响应标头中的“.code()”。

    在构建 Ion 请求时,请确保添加“.withResponse()”

    Ion.with(getContext())
    .load("http://example.com/test.txt")
    .asString()
    .withResponse()
    .setCallback(new FutureCallback<Response<String>>() {
        @Override
        public void onCompleted(Exception e, Response<String> result) {
            // print the response code, ie, 200
            System.out.println(result.getHeaders().code());
            // print the String that was downloaded
            System.out.println(result.getResult());
        }
    });
    

    根据 code() 响应,您可以手动抛出异常。

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2014-04-13
      • 2017-01-12
      相关资源
      最近更新 更多