Android 如果响应代码为';t 20x

Android 如果响应代码为';t 20x,android,ion,ion-koush,Android,Ion,Ion Koush,我正在使用Koush Ion库,我想知道如果响应代码不是20x(比如400、401等),是否有引发异常的选项。您只需在响应头中检查“.code()” 在生成Ion请求时,请确保添加“.withResponse()” Ion.with(getContext()) .加载(“http://example.com/test.txt") .asString() .withResponse() .setCallback(新的FutureCallback(){ @凌驾 未完成公共无效(异常e,响应结果){

我正在使用Koush Ion库,我想知道如果响应代码不是20x(比如400、401等),是否有引发异常的选项。

您只需在响应头中检查“.code()”

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

Ion.with(getContext())
.加载(“http://example.com/test.txt")
.asString()
.withResponse()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,响应结果){
//打印响应代码,即200
System.out.println(result.getHeaders().code());
//打印下载的字符串
System.out.println(result.getResult());
}
});
根据code()响应,您可以手动抛出异常

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());
    }
});