0

I'm using Koush Ion library, and I want to know if there is an option to throw an exception if the response code is not 20x (like if it's 400, 401 etc).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shalev Moyal
  • 644
  • 5
  • 12

1 Answers1

0

You can simply check for the ".code()" in the response headers.

While building the Ion request, make sure you add the ".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());
    }
});

Based on the code() response, you can manually throw the exception.

Ashwin Valento
  • 878
  • 1
  • 7
  • 14